6

My situation is I have a library that doesn't have a "lib" prefix. I'd like to link against it, and I can't recompile it (it's actually a Python module).

Now, if you use the '-l' flag with GCC or clang, then the lib prefix is automatically added and the library is not found. For GCC, I can use '-l:mylib.so' to get it to link against an arbitrary file.

However, this doesn't work for clang. Is it possible to get clang to link against a particular library without the 'lib' prefix?

user2333829
  • 1,301
  • 1
  • 15
  • 25

1 Answers1

6

This answers the question, but from a somewhat different angle. See the related question C++ 11 code compiles with `clang++`, but not with `clang -x c++`.

The gcc documention states:

Object files are distinguished from libraries by the linker according to the file contents.

This does not work if you use clang++ -x c++. Instead, the library file is taken as a C++ source file, and this generates a million or so compile errors. And you can't put the library file before the -x c++, because the needed symbols won't be linked in.

One obvious solution is to rename your source files to have a .cpp extension, so the -x switch is unnecessary (or use symbolic links). But this might be a pain to add to your build system.

Another solution, directly related to the OP's question, is to specify the library via the flags:

    -L. -l:mylib.foo

If the library is not in the local directory, change the dot . accordingly.

Community
  • 1
  • 1
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • See also [How to link using GCC without -l nor hardcoding path for a library that does not follow the libNAME.so naming convention?](http://stackoverflow.com/a/281253). – Joseph Quinsey Jul 30 '16 at 17:21