mans
mans

Reputation: 18168

glob_recurse generate error in cmake

I have a library with several source files under several directories. I want to write a cmake in a way that it add all of them to project without asking to writ them separately.

I am using this line in my cmake:

FILE(GLOB_RECURSE ALL_Lib_CPP_SRCS  src/Library/ *.cpp)
add_library(MyLibrary STATIC ALL_Lib_CPP_SRCS)

but generate msvc project doesn't have all files included and I am getting this message when I am running cmake:

CMake Error at CMakeLists.txt:49 (add_library):
Cannot find source file:

ALL_Lib_CPP_SRCS
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx

What is the problem with this cmake?

Upvotes: 0

Views: 1056

Answers (2)

Joel
Joel

Reputation: 2035

It should be:

FILE(GLOB_RECURSE ALL_Lib_CPP_SRCS "src/Library/*.cpp")
add_library(MyLibrary STATIC ${ALL_Lib_CPP_SRCS})

In my opinion, it's better that you defined manually the sources:

set (_SOURCES source3.cpp source2.cpp source1.cpp main.cpp)
# now use ${_SOURCES}

this way, you can know the exact order of compilation...sometimes the order is important

Upvotes: 1

Dark Falcon
Dark Falcon

Reputation: 44181

The correct syntax according to the manual is:

 file(GLOB_RECURSE variable [RELATIVE path] [FOLLOW_SYMLINKS] [globbing expressions]...)

I suspect you're either missing RELATIVE:

FILE(GLOB_RECURSE ALL_Lib_CPP_SRCS RELATIVE "src/Library/" "*.cpp")

Or you have an extra space in the globbing expression:

FILE(GLOB_RECURSE ALL_Lib_CPP_SRCS "src/Library/*.cpp")

I suspect the second option above is what you want. The quotes are optional, but I would recommend their use, as it makes the line easier to read IMO.


The second issue is that you need a ${} when you reference the variable:

add_library(MyLibrary STATIC ${ALL_Lib_CPP_SRCS})

Upvotes: 0

Related Questions