ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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
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
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.