Reputation: 3
I am trying to build a project (c++ code files) that is to be applied on different type of target machines, and I am trying to include cpp files according to the machine type, for this I created options in the CMakeLists file in order to use them to whether include the cpp file or not.
option (MACHINE1 "MACHINE1 DESCRIPTION" OFF)
option (MACHINE2 "MACHINE1 DESCRIPTION" OFF)
...
...
...
add_library (SO_LIBRARY
SHARED FILE1.cpp
if (MACHINE1)
FILE2.cpp
endif ()
if (MACHINE2)
FILE3.cpp
endif ()
)
...
I already have a linked bitbake file where I can set these options on and off, it is not the problem, the issue is that CMakeFile does not accept this type of writing :
| CMake Error at CMakeLists.txt:52 (add_library):
| Cannot find source file:
|
| if
|
| Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
| .hpp .hxx .in .txx
|
|
| CMake Error at CMakeLists.txt:52 (add_library):
| No SOURCES given to target: SO_LIBRARY
is there any possible way to do it ?
Thank you very much.
Upvotes: 0
Views: 1248
Reputation: 409
cmake_minimum_required(VERSION 3.1)
option (MACHINE1 "MACHINE1 DESCRIPTION" OFF)
option (MACHINE2 "MACHINE1 DESCRIPTION" OFF)
add_library(SO_LIBRARY SHARED)
if(MACHINE1)
target_sources(SO_LIBRARY
PRIVATE
FILE2.cpp
)
endif()
if(MACHINE2)
target_sources(SO_LIBRARY
PRIVATE
FILE3.cpp
)
endif()
In modern CMake (since 3.1) you should avoid using custom variables in arguments of project commands and prefer to use target_sources
Upvotes: 1
Reputation: 1223
Your goal is to create a list of sources which content will depend on some conditions.
Start with defining a variable and put there all common source files:
set(SOURCES
file_common_1.cpp
file_common_2.cpp
)
Then, APPEND elements to SOURCES
list depending on conditions:
if(OPTION1)
list(APPEND SOURCES file_specific_machine1.cpp)
endif()
if(OPTION2)
list(APPEND SOURCES file_specific_machine2.cpp)
endif()
Finally, use SOURCES
list to create a library:
add_library(SO_LIBRARY ${SOURCES})
Upvotes: 0
Reputation: 286
Try the following, Based on the type of the machine, assign the file name to a variable and then use it to add to sources.
if(MACHINE1)
set(SOURCEFILES "file1.cpp");
if(MACHINE2)
set(SOURCEFILES "file2.cpp");
if (MACHINE3)
set(SOURCEFILES "file3.cpp");
now add the file to add library
add_library (SO_LIBRARY
SHARED ${SOURCEFILES})
Upvotes: 1
Reputation: 623
I would do something along these lines instead of option
:
set(COMMON_SRC File1.cpp)
set(MACHINE1_SRC File2.cpp)
set(MACHINE1_SRC File3.cpp)
if(MACHINE1)
add_library (SO_LIBRARY SHARED ${COMMON_SRC} ${MACHINE1_SRC_SRC}
elseif(MACHINE2)
add_library (SO_LIBRARY SHARED ${COMMON_SRC} ${MACHINE2_SRC_SRC}
else()
...
endif()
Upvotes: 0