How to get clang to link against a library without the "lib" prefix?
Asked Answered
B

1

7

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?

Basra answered 29/1, 2015 at 16:35 Comment(5)
Just use the /full/path/name/to/your/module.libSyncom
@n.m. Thank you, that worked! Does the same work for GCC or is ':' the proper way to do it?Basra
works for every compiler and linker I know.Syncom
Okay, I don't think this is working (or something else is weird). I tried with GCC and clang, and get a "no library file corresponding to -l{mylibrary}", where {mylibrary} is the full path to a .so file without the lib prefixBasra
You may want to type man gcc in your terminal and scroll down to "Options for Linking", or look at it online.Syncom
P
7

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.

Purcell answered 30/7, 2016 at 17:11 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.