3

I am trying to build by source using the static version of the test library. I have both libtest.a and libtest.so available, so I am using the "-static" option. However, It looks like the gcc linker is also trying to search for static version the standard math library. Any idea what option I can use to link the shared versions of the standard libraries?

g++ -static main.cpp -o a.out -L. -ltest

Error:

/usr/bin/ld: cannot find -lm
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
Rajat
  • 467
  • 1
  • 5
  • 15
  • Do you have libtest.so in current directory? – billz Jan 24 '13 at 04:51
  • yes i do have both libraries in the current directory – Rajat Jan 24 '13 at 04:54
  • Do you have the static version of the math library somewhere, i.e. libm.a? – jogojapan Jan 24 '13 at 04:59
  • Unfortunately no. Also. "g++ main.cpp -o a.out -L. -ltest" works fine but then the shared version of the test library is linked into the executable. I want to link against the static libtest.a – Rajat Jan 24 '13 at 05:01
  • You need to install them -- best on the system level if you have the necessary permissions. Package managers do this (e.g. on Fedora `sudo yum install glibc-static`). – jogojapan Jan 24 '13 at 05:02
  • Your using the `-static` options means it tries to link everything statically, including libm and libc. – jogojapan Jan 24 '13 at 05:03
  • Oh.. So is there no way in gcc to link against the static version of one library and the shared version of the another library. – Rajat Jan 24 '13 at 05:03
  • Great. Thanks jogojapan. The "-Wl,-Bdynamic" option works perfectly. – Rajat Jan 24 '13 at 05:09
  • This question is not a duplicate of "cannot find -lc and -lm in g++ linux" even if the title was nearly the same. I've changed the title to be possibly be more descriptive of the actual question. – Michael Burr Jan 24 '13 at 15:02

2 Answers2

9

If you want to force the linker to use the static version of a particular library you can use the :filename to force a particular library instead of just giving the linker a 'base' library name and letting it use the first one it finds:

g++ main.cpp -o a.out -l:./libtest.a

From http://sourceware.org/binutils/docs-2.23.1/ld/Options.html:

-l namespec
--library=namespec

Add the archive or object file specified by namespec to the list of files to link. This option may be used any number of times. If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a.

On systems which support shared libraries, ld may also search for files other than libnamespec.a. Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a. (By convention, a .so extension indicates a shared library.) Note that this behavior does not apply to :filename, which always specifies a file called filename.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
0

I've never used Michael's suggestion, but I will be tucking it away for future use.

The technique I use to fully control library linking is to avoid -L, l, -Bstatic and -Bdynamic altogether by fully specifying the library I want to use. The command would look similar to:

g++ main.cpp -o a.out /usr/local/lib/test.a

or

g++ main.cpp -o a.out /usr/local/lib/test.so

or

g++ main.cpp -o a.out /usr/local/lib/test.so.1.0.0
jww
  • 97,681
  • 90
  • 411
  • 885