How to make Boost DLLs accessible to an executable built with CMake? - windows

I'm using CMake on Windows to build test suite based on Boost.Test. As I'm linking to Boost.Test dynamically, my executable needs to be able to find the DLL (which is under ../../../boost/boost_1_47/lib or something like that relative to the executable).
So I need to either copy the DLL into the folder where the executable is, or make it findable in some other way. What's the best way to achieve this with CMake?
-- Additional info --
My CMakeLists.txt has this Boost related configuration at the moment:
set(Boost_ADDITIONAL_VERSIONS "1.47" "1.47.0")
set(BOOST_ROOT "../boost")
find_package(Boost 1.47 COMPONENTS unit_test_framework REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})
add_executable(test-suite test-suite.cpp)
target_link_libraries(test-suite ${Boost_LIBRARIES})

Assuming you are running your tests by building the RUN_TESTS target in Visual Studio:
I always add .../boost/boost_1_47/lib to my command PATH environment variable, so the boost unit_test_framework dlls can be found at run time. That's what I recommend.
If for some reason changing your PATH is not possible, you could copy the files with cmake.
(untested)
get_filename_component(LIBNAME "${Boost_UNIT_TEST_FRAMEWORK_LIBRARY_RELEASE}" NAME)
add_custom_command(TARGET test-suite POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${Boost_UNIT_TEST_FRAMEWORK_LIBRARY_RELEASE}" "${CMAKE_CURRENT_BINARY_DIR}/${LIBNAME}"
)
3. If you are NOT only running the tests at build time (as I was assuming above), then you need a series of INSTALL commands, like Hans Passant suggested. In your snippet you don't have an INSTALL command for your executable; so even your executable won't end up "in the executable folder". First add a cmake INSTALL command to put your executable someplace in response to the cmake INSTALL target. Once you have that working, we can work on figuring out how to add another INSTALL command to put the boost unit_test_framework library into the same location. After that, if you want to make an installer using CPACK, the library will automatically be installed with the executable.

I ended up using the install command to copy the Boost DLL over to the executable's folder:
get_filename_component(UTF_BASE_NAME ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY_RELEASE} NAME_WE)
get_filename_component(UTF_PATH ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY_RELEASE} PATH)
install(FILES ${UTF_PATH}/${UTF_BASE_NAME}.dll
DESTINATION ../bin
CONFIGURATIONS Release RelWithDebInfo
)
get_filename_component(UTF_BASE_NAME_DEBUG ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY_DEBUG} NAME_WE)
install(FILES ${UTF_PATH}/${UTF_BASE_NAME_DEBUG}.dll
DESTINATION ../bin
CONFIGURATIONS Debug
)

I have a very similar problem, but the solution presented here is not really satisfactory.
Like the original poster, I want to run unit tests based on boost::test.
I have multiple test projects, one for each mayor component of our product.
Having to run the install target prior to every test means recompiling the whole thing just to run the tests belonging to a core component. That's what I want to avoid.
If I change something in a core component, I want to compile that core component and the associated tests. And then run the tests. When the tests succeed, only then do I want to compile and eventually install the rest of it.
For running the tests in the debugger, I found some very useful cmake scripts at :
https://github.com/rpavlik/cmake-modules
With this, I can specify all the directories of the required dlls, and the PATH environment variable is set for the new process:
# for debugging
INCLUDE(CreateLaunchers)
create_target_launcher(PLCoreTests
ARGS "--run-test=Core1"
RUNTIME_LIBRARY_DIRS ${PL_RUNTIME_DIRS_DEBUG} ${PROJECT_BINARY_DIR}/bin/Debug
WORKING_DIRECTORY ${PL_MAIN_DIR}/App/PL/bin
)
Where ${PL_RUNTIME_DIRS_DEBUG} contains the directories where the dlls from boost and all the other libraries can be found.
Now I'm looking for how I can achieve something similar with ADD_CUSTOM_COMMAND()
Update:
ADD_CUSTOM_COMMAND() can have multiple commands that cmake writes into a batch file. So, you can first set the path with all the runtime directories, and then execute the test executable. To be able to easily execute the tests manually, I let cmake create an additional batch file in the build directory:
MACRO(RunUnitTest TestTargetName)
IF(RUN_UNIT_TESTS)
SET(TEMP_RUNTIME_DIR ${PROJECT_BINARY_DIR}/bin/Debug)
FOREACH(TmpRuntimeDir ${PL_RUNTIME_DIRS_DEBUG})
SET(TEMP_RUNTIME_DIR ${TEMP_RUNTIME_DIR} ${TmpRuntimeDir})
ENDFOREACH(TmpRuntimeDir)
ADD_CUSTOM_COMMAND(TARGET ${TestTargetName} POST_BUILD
COMMAND echo "PATH=${TEMP_RUNTIME_DIR};%PATH%" > ${TestTargetName}_script.bat
COMMAND echo ${TestTargetName}.exe --result_code=no --report_level=no >> ${TestTargetName}_script.bat
COMMAND ${TestTargetName}_script.bat
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Debug
)
ENDIF(RUN_UNIT_TESTS)
ENDMACRO()
With this, the unit tests catch the errors as soon as possible, without having to compile the whole lot first.

Related

Run make install command in cmake from another cmake file

We have multiple libraries in different folder, The main application needs to build those libraries in other folders and install them to output folder and then the main application needs to link to libraries to build executable.
I am able to build the libraries present in other folders using add_subdirectory() in a loop, but I am not able to install them to output folder by main cmake file. Could anyone help me out on this.
The main application needs to build those libraries in other folders and install them to output folder and then the main application needs to link to libraries to build executable.
It is not necessary in CMake to install libraries in order to link to them. You can build the libraries and have your main executable link to them without installing the libraries. When you need to install your application as a whole, you can install libraries along with the executable if needed i.e. if the libraries are shared ones and not static ones.
One example of how you can organize things: assume you have the following structure in your project:
CMakeLists.txt # root of project
|
|--lib
| |--CMakeLists.txt # library subproject
|
|--app
|--CMakeLists.txt # app subproject
Then your root CMakeLists.txt can look like this:
project(MyProject)
add_subdirectory(lib)
add_subdirectory(app)
The lib subproject's CMakeLists.txt can look like this:
project(MyLib)
set(SOURCES <...>) # specify library's sources
add_library(${PROJECT_NAME} ${SOURCES})
set(MyLib ${PROJECT_NAME} CACHE INTERNAL "")
The last line in the snippet above is aimed to make MyLib variable available everywhere within the project. I found this trick here and used it successfully in my projects. Maybe there are better options here, if anyone knows them, feel free to suggest.
The app's CMakeLists.txt can then look like this:
project(MyApp)
set(SOURCES <...>) # specify app's sources
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} ${MyLib})
I haven't covered the installation here but it's actually straightforward: if your libraries are static ones, you only need to install the executable using install TARGETS. If your libraries are shared ones, you need to install them along with the executable.

CodeLite with CMake does not execute make when building

I just switched to CodeLite for C++ development.
I followed the QuickStart guide and created a simple console executable (g++), and when setting up a new project, you can choose the Build System, which offers the options
Default
CMake
GNU makefile onestep build
NMakefile for MSVC toolset
If I choose "CMake", and leave everything else the way it is, I get my usual hello world code in main.cpp.
Now I want to build it. I execute CMake, which produces a makefile, and then execute "Build" (by pressing F7 or via the menu). That does not work:
C:\Windows\system32\cmd.exe /C cd "\\"C\Users\Username\Documents\CodeLite\WorkspaceName\cmake-build-Debug"\ProjectName" && C:/mingw/mingw64/bin/mingw32-make.exe -j4 SHELL=cmd.exe -e
"\\C\Users\Username\Documents\CodeLite\WorkspaceName\cmake-build-Debug\ProjectName"
CMD unterst_tzt keine UNC-Pfade als aktuelles Verzeichnis.
====0 errors, 0 warnings====
The last line says it does not support UNC paths. Not sure whether that's the problem, but anyway, nothing actually gets built.
However, if I simply open a command line in the cmake-build-Debug\ProjectName\ directory, I can execute "make" and my executable is built properly.
Now my question is: Why does CodeLite not build my project? This does not seem to work as intended. I did not change any settings and my project is a clean default, but yet, I have to build it manually.
If I simply choose "Default" instead of "CMake", it gets build, by the way.
A path in UNC (universal naming convention) has \\ before the host name and then the drive, directory, ... letters.
The path "\\C\Users\Username\Documents\CodeLite\WorkspaceName\cmake-build-Debug\ProjectName" does not look like a proper (UNC or other) path. Was it written this way by the CodeLite build system or did you need to do this manuully?
The form "C:\Users\Username\Documents\CodeLite\WorkspaceName\cmake-build-Debug\ProjectName" (or "C:\Windows\Users\ ...") should work on your system.

Shared library under Windows and CMake: DLL not found before installation

The library mylib consists of the library proper, in directory lib/, and a test suite, in directory test/. It is completely under CMake control:
mylib/CMakeLists.txt:
...
add_subdirectory(lib)
add_subdirectory(test)
...
mylib/lib/CMakeLists.txt:
...
add_library(my_lib ${src_files})
...
mylib/test/CMakeLists.txt:
...
add_executable(mytest mytest.c)
target_link_libraries(mytest mylib)
Build steps are:
mkdir build
cd build
cmake ..
make
ctest # or make test
make install
Works under Linux, stable since many years. Under Windows10 though, a message window pops up, entitled "mytest.exe - System error": "The code execution cannot proceed because mylib.dll was not found. Reinstalling the program may fix this problem."
No, installing (rather than reinstalling) would not be a good solution: I need to first test the library before I install it (btw: this excludes most solutions proposed in response to somewhat similar questions).
Isn't CMake supposed to work cross-platform? What is the minimally invasive adjustment to make the above build steps work under Windows?
The right way of doing this on Windows is to populate the PATH environment variable for the test run:
set_tests_properties(your_test_name
PROPERTIES
ENVIRONMENT PATH="path-containing-your-dll")
I believe you can use generator expression if path-containing-your-dll is a function of an artifact that you generate in your build.
Cherry on top: since cmake 3.13, the variable VS_DEBUGGER_ENVIRONMENT can also be set on the target for having a nice debugging behaviour inside Visual Studio (eg. being able to debug the application directly from Visual instead of going through ctest).

Compile & Run CLion Project from Terminal

I am trying to create generally-accessible compile & run instructions for my CLion project, but can't find the exact terminal command it uses to execute the program (it's makefile, I would assume). From the project directory in the terminal, how would I do this?
The directory looks like this:
I will add a little bit to #Stanley F.'s excellent answer.
FROM the root of the CLion project, this is what works for me. I generally run with a debug profile. The same can be reproduced for release.
When cmake loads its project, it runs
cmake -Bcmake-debug-build -H. ${CMakeOptions}
where CMakeOptions is stored in CLion at
CLion->settings->Build,Execution, Deployment->CMake->[profile]->CMake Options
My general cmake build option is
-DCMAKE_BUILD_TYPE=debug -DSYTEM_ARCH=Linux-gcc5.3.0-x86_645 -CMAKE_CXX_STANDARD=14
[Note the lower-case d for 'debug'. If I do not use this, my system will not work. I wish that CLion did not default to 'Debug']
So, to reproduce what CLion creates upon project reload, I run
rm -rf cmake-debug-build
cmake -Bcmake-debug-build -H. -DCMAKE_BUILD_TYPE=debug -DSYTEM_ARCH=Linux-gcc5.3.0-x86_645 -CMAKE_CXX_STANDARD=14
Then, to build the project, I run
cmake --build cmake-build-debug --target all
Please note that when I run the first cmake command (from CLion or the command line), cmake pulls in lots of libraries from other "precedent" projects as part of the processing of my CMakeLists.txt file. If anything in one of those precedent projects changes, I will not pull them in anew, unless I physically delete the entire cmake-build-debug/ directory. None of CLion's reset tool menu items from my experience will delete that file.
If I am running these commands from the CLion menus, then I have to physically delete the cmake-build-debug/ directory as well (if I have a change in one of the external libraries that I want to pull in).
CLion currently only supports CMake projects. So you have to invoke the CMake executable with the appropriate parameters for your project.
At first, you can delete the cmake-build-debug folder, since this is auto-generated by CLion, which itself invokes CMake. It only contains temporary files.
So your build environment basically contains the 3DTable.c, 3DTable.h and CMakeLists.txt files. At least this is what I get from the screenshot.
To build the project from command line, first navigate to the source directory. Then invoke CMake:
cd <source path of Project_1>
cmake -Bbuild -H.
cmake --build build --target all
Notes:
build is the directory, where CMake will generate temporary files and the build artifacts.
The -H. option tells CMake, where the CMakeLists.txt file is located, which in this case is the current working directory.
The library / executable for your project will be located within the build directory
CLion can tell you, you don't need to hunt.
CMake command line
Select tools\cmake\reload cmake project.
The command line is shown in the CMake window.
Build command line
Select build\build project.
The command line is shown in the messages window.
Example
Mine look like this:
"C:\Program Files\JetBrains\CLion 2021.2.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2021.2.2/bin/ninja/win/ninja.exe" -G Ninja -S C:\some_application -B C:\some_application\cmake-build-debug
...
"C:\Program Files\JetBrains\CLion 2021.2.2\bin\cmake\win\bin\cmake.exe" --build C:\some_application\cmake-build-debug --target all -j 9
Reminder
If using Visual Studio you still to specify which environment you are using. Typically this involves using the VS command prompt or executing one of the premade scripts to set up the environment variables. See here.

running cpp project with cmake (mac)

My question is very naive. Every time I ran an algorithm, it's with an IDE so I don't know exactly what is generated in backgrown.
I try to run with my MacBook an algorithm that I didn't write and I'm blocked !
In the readme file: they said "Use cmake to generate desired projects on different platforms. (See “CM.txt” in the
“src” folder)"
In the CM.txt:
project(BlProj)
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
add_executable(BlProj
main.cpp
thi.cpp
thu.cpp
mat.cpp
Config.cpp
Stringer.cpp
)
I did: cmake CM.txt
A CM directory is generated. I will not list all the files inside: it's some cmake, bin, out, cpp, c, cxx, log, txt files.
I don't know what I can do with that!
I'm supposed to generate a compiled binary and run it with the command:
./BlProj data1 data2 10 config output
But it wasn't generated...
So I don't know how I can have the BlProj executable. Could you help me?
How do the cpp compilation work? What did cmake exactly?
Is cmake CM.txt is a good practice or not?
You can use cmake-gui to build sources.
go to CMAKE installation directory and inside bin folder run cmake-gui
Provide input where the source code is and provide an input where to build.
hit on configure and then generate.
you might need to select IDE version as well as provide additional input, it depends.
once done open generated project files with your IDE and build.

Resources