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

Time:11-08

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

CodePudding user response:

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.

CodePudding user response:

My bad, I just need to remove:

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

and this will work.

  • Related