LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2008, 02:10 PM   #1
zensunni
Member
 
Registered: Feb 2008
Posts: 45

Rep: Reputation: 0
Compiler questions (g++ specific)


Now that I've started using c++ libraries like ncurses and gtkmm, there's a few questions that I've been wondering about.

1. When linking to libraries, how does the compiler know where to link? IE, how would I link to my own libraries?

For example, when I use ncurses, I go "g++ -lncurses -o boot main.cpp object.cpp". In pirute, I can see that ncurses is installed, but when I link to it, it won't work. Only when I instal ncurses-devel does this work. So, what in ncurses-devel allows me to link?

2. How do I make my program portable to other linux machines? I've noticed with most installs, you have to run a configure file, then run a makefile. I've seen a little bit about makefiles, but other than that, I've no idea how to make sure your program can run on another machine.

Last edited by zensunni; 03-09-2008 at 02:11 PM.
 
Old 03-09-2008, 02:37 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
First you should learn to write makefiles (man make.) Once you get a good grasp on the conditional build concept, you need to learn the auto-toolchain which consists of autoconf, automake, and libtool. Those together generate ./configure files, which in-turn generate makefiles specific to the machine you are compiling on. The makefiles generated then build the binary code and install it as configured.

Move -lncurses to the end of your compilation line. I think what's happening is g++ looks at that library first and basically discards it, then gets to your source code. With it at the end, your source code is compiled and g++ sees that it's missing symbol definitions, so it will look in libncurses.so for those definitions.
ta0kira

Last edited by ta0kira; 03-09-2008 at 02:39 PM.
 
Old 03-09-2008, 02:41 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by zensunni View Post
1. When linking to libraries, how does the compiler know where to link? IE, how would I link to my own libraries?
The linker will link to libraries in the library path (using LD_LIBRARY_PATH or the values in /etc/ld.so.conf), or you may specify the path directly with the -L/my/special/path directive to the linker (if you give it to the compiler to create an executable, the compiler will automatically call the linker for you and pass the option transparently). When you specify a library to be linked as “-lhello” the linker looks for a file named “libhello.so” or “libhello.a” in the aforementioned path (depending on whether you have specified dynamic or static linking).
Quote:
Originally Posted by zensunni View Post
For example, when I use ncurses, I go "g++ -lncurses -o boot main.cpp object.cpp". In pirute, I can see that ncurses is installed, but when I link to it, it won't work. Only when I instal ncurses-devel does this work. So, what in ncurses-devel allows me to link?
This was most likely not a problem in linking, but a problem in compiling. The compiler needs to find the headers that you include in your source. The headers are only useful at compile-time (not at runtime), so for simplicity, most distros put the headers into a separate package. If you want to control include-file path lookup, you can use the -I/my/special/path directive to the compiler.
Quote:
Originally Posted by zensunni View Post
2. How do I make my program portable to other linux machines? I've noticed with most installs, you have to run a configure file, then run a makefile. I've seen a little bit about makefiles, but other than that, I've no idea how to make sure your program can run on another machine.
There are multiple portable source makefile systems. They include autoconf (used by most GNU software—this is the system in which you have configure scripts), SCons, CMake, and others.
 
Old 03-09-2008, 07:23 PM   #4
zensunni
Member
 
Registered: Feb 2008
Posts: 45

Original Poster
Rep: Reputation: 0
Thanks for the info. This is really helpful. The info you gave led me to this:

http://www.faqs.org/docs/Linux-HOWTO...ary-HOWTO.html

One last quick question:

Is there a header file path for compiling just like a library path? I type "include.h" in my program to include it, but I don't know where it comes from, or how the compiler finds it. Are they virtual?

Thanks again.

Last edited by zensunni; 03-09-2008 at 08:39 PM.
 
Old 03-10-2008, 10:06 AM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by zensunni View Post
Is there a header file path for compiling just like a library path? I type "include.h" in my program to include it, but I don't know where it comes from, or how the compiler finds it. Are they virtual?
I already mentioned this in my other post, but you use the “-I/my/special/path” syntax.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to specify a specific compiler version? raid517 Linux - Software 7 06-18-2006 09:33 AM
Specific questions regarding parted Atreyou40 Linux - Software 1 06-06-2006 07:10 PM
Some specific questions about RPM programming zhouxjgester Linux - Software 0 09-21-2005 01:52 PM
a few specific questions about (SW) RAID 5 malo_umoran Linux - Hardware 9 01-31-2005 12:56 AM
specific questions regarding install = upgrade Private Citizen Linux - Software 0 10-05-2004 04:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration