Linking to libraries in the current directory

This is a discussion on Linking to libraries in the current directory within the C Programming forums, part of the General Programming Boards category; Hi, I'm developing an application that hosts the Mono runtime on linux, so it has a dependency to libmono.so. The ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Question Linking to libraries in the current directory

    Hi,

    I'm developing an application that hosts the Mono runtime on linux, so it has a dependency to libmono.so. The application only has one file and I compile it with the following command:

    gcc -o HostApp main.c `pkg-config --cflags --libs mono`

    This way I'm linking against the system libraries, however I plan to deploy this app without requiring mono installed on the system, so I want to place my own version of libmono.so(which I compiled from svn) in the executable's dir and force the HostApp to link against it instead of looking for it in the system. Anyone has any idea on how I should proceed?

    Thanks in advance.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You'll have to check all the flags that pkg-config calls there (just run "pkg-config --cflags --libs mono" on the command line). Presuming it's just one (-lmono), the normal lib path includes the current directory.

    You could also compile it in, since you have the source. This would be like a standalone executable.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    The test that made me doubt was this :

    1- I copied libmono.so to the source files directory
    2- Renamed libmono.so to libmono123.so
    3- Compiled : gcc -o HostApp main.c -L. -lmono123 `pkg-config --cflags mono`

    This compiled fine so I tought : "The HostApp executable must have a reference to a file named libmono123.so which only exists in its directory"
    HostApp runs fine after this compilation, however if I remove the libmono123.so from the directory the HostApp still runs, which makes me think it is still compiled against libmono.so from the system.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Thiago View Post
    it is still compiled against libmono.so from the system.
    For sure. Do you understand what `pkg-config --cflags mono` means? `backticks` are a shell shorthand -- it means to substitute the output of the command here.

    pkg-config --cflags mono is a shell command. Try it from the command line. All it does is output a list of compiler switches, generally a set of extensions to the include path and of course linkages, eg:

    [user@commandprompt] pkg-config --cflags somelibpack
    -I/usr/local/share/somelibpack/include -I/usr/local/share/someotherpack/include -lsomelib -lsomeother

    So `pkg-config --cflags mono` substitutes all that before gcc is even called. If you use it, you will be linking to the mono distribution you have installed! If you don't want to do that, you cannot use pkg-config!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    To my understanding the -I compiler option only passes the locations of the header files(which contains the definitions) thats why I only use the -cflags on pkg-config, if I did:
    `pkg-config --cflags --libs mono` then I would be referencing the system installed libraries.
    Correct me if I'm wrong, I'm new to C programming so thats what I understood when I first read about GCC

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    Ok, I managed to find out what was wrong using the 'ldd' command

    It seems the lib filename is specified inside the library itself and not by the system, so when linking against libmono.so or libmono123.so I was actually linking to libmono.so.0(which must be defined in the library image).

    However, renaming the file to libmono.so.0 was not enough, I had to specify the library search path at linking time with the following :

    gcc -o HostApp main.o -Wl,-rpath=. -L. -lmono

    -Wl flag pass options to the linker, in this case the 'rpath' option which specify the runtime linker search path.
    Compiling the application like that will make the runtime linker to search the current directory for the library so if I have no libmono.so.0 in the current dir, the linker will look in the standard system directories.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Thiago View Post
    To my understanding the -I compiler option only passes the locations of the header files(which contains the definitions)
    The -l 's are the complied library executables (eg, .so), not the headers. The headers for those are #included by you in your code.

    I'm not really sure why pkg-config adds to the include path (which is the path the compiler uses to look for #include <files>), since a pre-compiled library cannot itself require the use of a header. In fact, I just trying compiling a gtk app (which uses `pkg-config --cflags --libs gtk+-2.0`) without pkg-config by copying just the -l switches from the pkg-config output. Works fine, which is probably good news for you.

    `pkg-config --cflags --libs mono` then I would be referencing the system installed libraries.
    Very definitely if you use pkg-config directly you will be linking to the installed system libraries. Did you try running pkg-config from the command line to see what libraries those are? Post the output here.

    If -lmono is the only switch used then your solution should be good.
    Last edited by MK27; 06-09-2010 at 10:30 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    By linking to mono you are dragging a lot of extra libraries around....just be sure its worth it.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 10:52 PM
  2. How to get the current directory and the file name
    By megareix in forum Windows Programming
    Replies: 6
    Last Post: 01-08-2009, 10:01 PM
  3. Detect Current Directory
    By jmprox in forum C Programming
    Replies: 17
    Last Post: 10-02-2008, 12:52 PM
  4. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-02-2007, 12:48 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21