1

The correct link command is

g++ file1.o file2.o xxx.0 -o target -I./ -I/usr/local/libmylibone/ -L./ -L/usr/local/testlib/ ../lib/special_lib/static_lib.a -lasn1c++ -lmysqlclient -lnsl -lm -lz -lc -ldl -lpthread -lrt -ljson 

Please focus on ../lib/special_lib/static_lib.a, this is a static library and not named with libxxx.a. And I don't know how to write a CMake command to get this correct link command.

I've tried TARGET_LINK_LIBRARIES(../lib/special_lib/static_lib.a) and it will be translated to -l../lib/special_lib/static_lib.a. I've also tried TARGET_LINK_LIBRARIES(static_lib) but got -lstatic_lib and failed.

1 Answer 1

3

If you put the absolute path to your library it should work:

TARGET_LINK_LIBRARIES(your_binary /usr/local/lib/static_lib.a) 

Second option:

ADD_LIBRARY(staticlib STATIC IMPORTED) SET_TARGET_PROPERTIES(staticlib PROPERTIES IMPORTED_LOCATION /usr/local/lib/static_lib.a) TARGET_LINK_LIBRARIES(your_binary staticlib) 

The official CMake documentation for importing/exporting targets is here.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but could you give me more info about how to use abs path ? Actually I've tried, but the link command cmake gives me is "g++ files.o -o binary L/my/absolute/path/to/static_lib -lstatic_lib", and it doesn't work.
It should not give you this command. I have linked the official documentation, have a look at it.
@oyjh Which version of CMake do you use? Is it 2.4 or older? See CMake's policy CMP0003.
@Florian I'm using cmake3.3.0. Thanks for your link, it's helpful.
@Florian I've just tried use abs path in TARGET_LINK_LIBRARIES and it works fine. And it will fail to generate the link command when I use relative path.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.