简体   繁体   中英

How to create a static library (not executable) in CMake?

I'm new to cmake and I was wondering how to create a static library. In gcc, it can be done by:

ar rsv

Well, how do you do it using CMake?

add_library(mylib STATIC file1.cpp file2.cpp)
add_executable(myexe main.cpp)
target_link_libraries(myexe mylib)

This generates a static library (.a file) but how do you compile it without adding an executable? if I remove add_executable(myexe main.cpp), it gives me an error. I only want this file:

mylib.a

and NOT

myexe.exe
mylib.a

My bad, I just need to remove:

add_executable(myexe main.cpp)
target_link_libraries(myexe mylib)

and this will work.

add_library can be used by itself, without using add_executable at all. Simply remove line 2 to get rid of the executable. The error is most likely caused by line 3, which needs myexe to function. Line 3 should also be removed, because you are only building the library and not linking it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM