This question already has answers here:
Cmake add command line argument to binary
(2 answers)
Catch lib (unit testing) and CTest (CMake) integration
(2 answers)
Closed 1 year ago.
I have created small project for using catch2 testing framework. I set the specific command line option called --use-colour to yes in terminal like below:
root#ramil-instance:~/testing/build# ./testing --use-colour yes
How can I define this command line option in CMakeLists.txt file instead of terminal
This is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.0)
set(CMAKE_BUILD_TYPE Debug)
project(testing)
add_executable(
testing
test.cpp
)
Related
This question already has an answer here:
CMake globbing generated files
(1 answer)
Closed last year.
In the below minimal example, if I know the name of the generated source file in advance (test1.txt), I could do the below
cmake_minimum_required(VERSION 3.20)
project(example)
add_executable(example main.cpp ${CMAKE_BINARY_DIR}/test1.txt)
set_source_files_properties(${CMAKE_BINARY_DIR}/test1.txt PROPERTIES GENERATED TRUE)
However, how can I achieve the same thing if I don't know the name of the generated files in advance? For my application, it will also work if the generated files are added to another target instead of the original one. Please see below of my failed attempt
cmake_minimum_required(VERSION 3.20)
project(example)
add_executable(example main.cpp)
add_custom_target(
example-generate
COMMAND $<TARGET_FILE:example>
COMMAND "using bash commands to glob the generated files but need to find a way to output the result to a cmake variable"
DEPENDS example)
add_library(example2 OBJECT) # the target with the generated unknown sources, but I can't find a way to add sources to it in build time
add_dependencies(example2 example-generate)
You can generate a dummy source file with a concrete name that #includes all the others.
You might need to write out a DEPFILE to get accurate incremental-build dependencies (if the list of files depends on something other than the example sources/binary or command-line invocation).
This question already has an answer here:
Hinting Find<name>.cmake Files with a custom directory
(1 answer)
Closed 2 years ago.
I want every CMake project that uses boost (or any other lib) to find it in custom directory, for example /home/someuser/mylibs or C:/mylibs.
To achieve this I may add in CMakeLists.txt following command:
list(APPEND CMAKE_PREFIX_PATH "/home/someuser/mylibs")
This is not very comfortable when I cooperate with different people on different projects. The question is: can I use some environment variable to set it or there's another way to do this?
The usual way is to add -DCMAKE_PREFIX_PATH=/path/to/boost/ when calling CMake to configure your project. But, of course, you can also set an environment variable, e.g. BOOST_DIR and then read it out using CMake:
list(APPEND CMAKE_PREFIX_PATH $ENV{BOOST_DIR})
This question already has an answer here:
CMake - always build specific file
(1 answer)
Closed 2 years ago.
I have a C++ file using __DATE__ to display the build date of my app. But if this file is not modified, it will not be rebuilt and the date will not be updated.
Can CMake always rebuild that specific file?
Apparently it's possible with makefile :
How do you force a makefile to rebuild a target
Edit:
Duplicate of CMake - always build specific file
The solution you trying to apply is a bit against CMake principles since it might lead to the rebuilding of all dependant targets.
However, you can achieve this with an approach like this
add_custom_command(TARGET ${PROJECT_NAME}
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_CURRENT_SOURCE_DIR}/path/file.c)
Update with the 2nd approach:
add_custom_target(file_toucher
COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_CURRENT_SOURCE_DIR}/path/file.c)
add_dependencies(${PROJECT_NAME} file_toucher)
This question already has answers here:
CMake linking against shared library on windows: error about not finding .lib file
(1 answer)
Error LNK1104: cannot open file 'Debug\MyProjectLib.lib'
(2 answers)
Closed 3 years ago.
So I try to link an executable to my library, which is a shared library. But CMake (target_link_libraries) tries to link to a static library.
My folder structure looks like this:
sage/
|-CMakeLists.txt
|-src/
|-include/
|-tests/
|-CMakeLists.txt
|-sandbox/
|-CMakeLists.txt
|-src/
The library is called "sage". I have a "tests" directory for projects to test the library.
This is the CMakeLists.txt for the library:
cmake_minimum_required(VERSION 3.13)
project(sage VERSION 0.1.0)
set(SAGE_SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/")
file(GLOB_RECURSE SAGE_SOURCE_FILES "${SAGE_SOURCE_DIRECTORY}/*.cpp")
add_library(sage SHARED ${SAGE_SOURCE_FILES})
target_include_directories(sage PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/tests")
This is the CMakeLists.txt in test/
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/sandbox")
And this is the one in test/sandbox/
set(SANDBOX_SOURCE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/")
file(GLOB_RECURSE SANDBOX_SOURCE_FILES "${SANDBOX_SOURCE_DIRECTORY}/*.cpp")
add_executable(sandbox ${SANDBOX_SOURCE_FILES})
target_link_libraries(sandbox PRIVATE sage)
When I use "cmake --build .", I get the error that "sandbox" can not be linked to "sage.lib", because it is not found.
LINK : fatal error LNK1181: Input file "..\..\Release\sage.lib" can not be opened. [C:\Users\Daniel\Documents\Entwicklung\Cpp\sage\build\tests\sandbox\sandbox
.vcxproj]
But I defined the "sage" target to be a shared library, thus it should link to "sage.dll".
I am on Windows unsing the Visual Studio 2019 compiler.
This question already has answers here:
Passing an argument to CMAKE via command prompt
(2 answers)
Closed 3 years ago.
Not sure the right way to ask this, but basically, I have a project that I need to build in two slightly different ways. I could have two CMake files and keep them in sync, but I'd prefer to just have one and control it via a command line flag.
Something like cmake --configure . --flag vs cmake --configure ..
And then in the CMakeFile:
ifdef(flag)
line_that_is_different
endif()
to toggle the line on/off.
Obviously this isn't valid CMake, but is there some way to do this in Cmake?
From the command line:
cmake -Dflag=true
cmake -Dflag=On
cmake -Dflag=1
and in your cmake script:
if(flag)
message(STATUS "FLAG IS ON")
endif()
See the cmake manual and cmake if command.