Currently, I have CMakeLists and hoge.cpp in a directory, and running CMakeLists and make command generates hoge executable file.
Now I added hoge2.cpp and want to be able to generate two different hoge and hoge2 executable files by running CMakeLists and "make hoge" and "make hoge2" commands.
How can I do this?
Create two build targets in your CMakeLists.txt file.
add_executable( hoge hoge.cpp )
add_executable( hoge2 hoge2.cpp )
Then you can run (from same directory as your CMakeLists.txt file,
cmake --build . --target hoge
For the other build target use
cmake --build . --target hoge2
You can refer to the CMake documentation or the man pages for more information. Try running CMake with just the --build flag to get help.
Related
I am compiling a rather big C++ project with cmake.
The project has several targets defined in the CMakeLists.txt of the various subdirectories
I can create a folder somewhere and use cmake to prepare compilation of the targets, and compile any of the target :
cmake ..
make -j8 cytosim
And this works.
However, if I call cmake with a target :
cmake .. --target cytosim
I get the error
CMake Error: The source directory ".../build/cytosim" does not exist.
I thought target was supposed to be a module/library/executable target... What am I missing ?
CMake --target argument is usable in CMake Build Mode (--build).
Try cmake --build .. --target cytosim
this might be a duplicate, but I've already spent a couple of hours searching for an answer... without solution. First of all I know this problem might not exist if I'd use a Linux, but I am on Windows.
I am pretty new to c++ but already got some experience with java and gradle. I try to use cmake just like I am used to use gradle. I already read the cmake wiki, but I either do not find the correct pages or I just don't understand it. Here is my directory structure:
MyProject
-bin
-include
--header1.h
--header2.h
--header3.h
--header4.h
--header5.h
--header6.h
-src
--CMakeLists.txt
--MyProjectConfig.h.in
--impl1.cpp
--impl2.cpp
--impl3.cpp
--impl4.cpp
--impl5.cpp
--impl6.cpp
-main.cpp
-CMakeLists.txt
My CMakeLists.txt in my project folder looks like:
cmake_minimum_required (VERSION 3.14)
project (MyProject)
add_subdirectory(src)
file(GLOB_RECURSE sources src/*.cpp include/*.h)
# The version number.
set (Tutorial_VERSION_MAJOR 0)
set (Tutorial_VERSION_MINOR 1)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
"${PROJECT_SOURCE_DIR}/MyProjectConfig.h.in"
"${PROJECT_BINARY_DIR}/MyProjectConfig.h"
)
# add the binary tree to the search path for include files
# so that we will find MyProjectConfig.h
include_directories("${PROJECT_BINARY_DIR}")
install (FILES "${PROJECT_BINARY_DIR}/MyProjectConfig.h"
DESTINATION include)
add_executable(MyProject main.cxx ${sources})
# add the install targets
install (TARGETS MyProject DESTINATION bin)
My CMakeLists.txt in the src folder looks like:
cmake_minimum_required (VERSION 3.14)
include_directories(${MyProject_SOURCE_DIR}/MyProject/include)
I use the command in the bin bin directory: cmake -G "MinGW Makefiles" -S ../src
I got 2 questions now:
(How do I tell cmake to always use MinGW? ( I don't want to use -G always)) solved
(The compiled file build\CMakeFiles\3.14.0-rc2\CompilerIdCXX\a.exe does not have the expected behavior. It should print "Hello world!" and "My Class", while "My Class" is printed from the attribute of a class created from impl1.cpp, however it does nothing.) needs clarification:
How do I build a windows .exe-file to ruin on the console?
Edit:
I have learned that I have to call cmake --build . in my bin directory after creating the cmake files. However I just don't get an exe-file. With flag -v I get this output:
"C:\Program Files\CMake\bin\cmake.exe" -SD:\git\MyProject\src -BD:\git\MyProject\bin --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start D:\git\MyProject\bin\CMakeFiles D:\git\MyProject\bin\CMakeFiles\progress.marks
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 all
mingw32-make.exe[1]: Entering directory 'D:/git/MyProject/bin'
mingw32-make.exe[1]: Nothing to be done for 'all'.
mingw32-make.exe[1]: Leaving directory 'D:/git/MyProject/bin'
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start D:\git\MyProject\bin\CMakeFiles 0 ```
Using the -G option is the standard way of doing it. It prevents you from having to put system-specific settings into your CMake config (like the hardcoded paths to MingW) and lets you use other compilers without having to change build scripts.
The a.exe you started is not your build output. It should be called MyProjectExec.exe. Also, you need to specify all source files in your call to add_executable. add_subdirectory does not automatically add any source files (to what build output should it add them?), it just executes the CMakeLists.txt.
I am writing a library that contains library itself and examples and I am using CMake:
cmake_minimum_required(VERSION 3.6)
add_executable (example main.cpp)
install(DIRECTORY include DESTINATION include PATTERN ".DS_Store" EXCLUDE)
When I am running cmake --build . --target install - it compiles example target and makes installation of include directory
I want to exclude building example target and make only include directory installation when building install target and building example if running without any special target:
Here I want example to be NOT built:
cmake --build . --target install
Here I want example to be built:
cmake --build .
How should I change my CMakeLists.txt to make it work as I want?
You cannot exclude single CMake target when installing.
The problem is that 'install' target may depends only from 'all' (default) target.
While you may remove 'install' -> 'all' dependency (by setting CMAKE_SKIP_INSTALL_ALL_DEPENDENCY variable), you may not add another dependency for 'install'.
So, before installing
cmake --build . --target install
either performs
cmake --build .
or doesn't build anything (even library, which you want to build in any case).
Currently, I have a CMakeLists.txt file in the main folder that has the following code in it:
cmake_minimum_required(VERSION 3.5)
SET(CMAKE_GENERATOR "MinGW Makefiles" CACHE INTERNAL "" FORCE)
SET(CMAKE_TOOLCHAIN_FILE ${PROJECT_SOURCE_DIR}/ToolChain.cmake)
project(Blinky)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
...
When I run it, it outputs first for Visual Studio to the source directory. When I run it the second time, it outputs the corresponding minGW makefiles but still to the source directory and not the bin folder. Is there any way to configure it to build for MinGW Makefile directly and to the correct output folder?
I'm running the script on a command line prompt with the following line of code from the source directory folder:
cmake CMakeLists.txt
Run cmake from the directory you want to use as your build directory, not from within your source tree. That will give you an out of source build (see here for some details about this).
You have to set the CMake generator and toolchain file you want to use on the command line, you don't do it within the CMakeLists.txt file. Also, you do not include the name of the CMakeLists.txt file on your cmake command line, but rather the directory it is in. For example:
cmake -G "MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE=/path/to/source/dir/ToolChain.cmake /path/to/source/dir
Lastly, for specifying where your executables should go, make sure you use the correct CMake variables and make sure you put them in a place within your build directory, not your source tree:
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
You shouldn't run cmake from your source directory. It's definitely a bad practice
Indeed, that mixes your makefiles (or your Visual Studio files) with your source files and that can even corrupt your source directory (depending on what you've specified in your CMakeLists.txt).
What you have to do
First you need to create a separate build directory where you'll launch cmake.
Then, if you want to generate with MinGW makefiles, launch in the build directory the following command line:
cmake path_to_source_directory -G "MinGW Makefiles"
Further comments about your CMakeList.txt
As the command line above specifies the generator (option -G "…"), SET(CMAKE_GENERATOR "MinGW Makefiles" CACHE INTERNAL "" FORCE) is useless. Take a look on this SO post and its answers. It explains why you generate Visual Studio files on your first launch of CMake and MinGW makefiles on your second launch.
Then, as #Craig Scott said, you should replace SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) by SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin). Otherwise your executables will be put in your source directory.
I am quite new to linux and c++.. i have couple of cpp file and cmakefile.txt in my source folder. how can i compile in ubuntu with g++ (multiple cpp files)
I have this CMakeList.txt
project(Test)
# link_libraries($Nest_LIBRARIES})
subdirs(
#
engine
options
ui
# jni
)
#build the Test library
add_library(test STATIC
options/command_line_options.cpp
options/options_map.cpp
utility/timer.cpp
utility/generics/any.cpp
util/hdfs.cpp
logger/logger.cpp
logger/backtrace.cpp
)
requires_core_deps(test)
INSTALL(TARGETS
test ARCHIVE DESTINATION lib)
This is CMake (tutorial). Some say it simplifies build process.
Create makefiles from CMake metafiles: cmake . -G "Unix
Makefiles"
Run make: make -j2 install