CMake: Is it possible to build an executable from only static libraries and no source?

前端 未结 2 1324
粉色の甜心
粉色の甜心 2020-12-10 13:52

I would like to build an executable from static libraries (i. e. .a-files) only. This is possible, because the main() function is contained in one of these libr

2条回答
  •  轮回少年
    2020-12-10 14:14

    There is no way to do it without a hack. You need at least one *.c or *.cpp file.

    What I do is make a dummy null.cpp file (zero bytes) and use that. You can also use /dev/null but that only works on Linux.

    file(WRITE null.cpp "")
    
    add_executable(tester
        null.cpp
    )
    
    target_link_libraries(tester
        -Wl,--whole-archive
        libtest1
        libtest2
        libtest3
        libtest4
        -Wl,--no-whole-archive
        gtest_main
    )
    

提交回复
热议问题