Using CMake 2.8 on Windows 10, Intel compiler.
I'm building all 4 combinations of {debug, release}, {32bit, 64bit}, and want all build intermediates (e.g. obj) to be placed in 4 separate folders accordingly, relative to the project folder.
Tried setting:
CMAKE_CURRENT_BINARY_DIR
CMAKE_BINARY_DIR
PROJECT_BINARY_DIR
CMAKE_CACHEFILE_DIR
CMAKE_RUNTIME_OUTPUT_DIRECTORY
To no avail, and all the products end up in the project folder (where the CMakeLists.txt resides).
All build artifacts are placed in the directory cmake was run from (NOT necessarily where CMakeLists.txt resides).
So, for each configuration simply CD into the desired output directory and run cmake from there.
Related
I have a git repo with many many projects in it. I am trying to use MSVC with Clang-cl and CMake, Git.
I started out building a project directly in MSVC:
CMakePresets.json
CMakeLists.txt
asource.file.cpp
When I was happy with the working code, I moved these files into /repo/subfolder, and committed them.
Now I am cloning my repo as a new project: I chose to "clone repository" at MSVC project creation, and I want to avoid building the /repo root CMake project. Instead I want to build the /repo/subfolder CMake project. How can I achieve this?
Imagine I am on the Linux command line. Instead of:
cd myrepo
mkdir -p build && pushd build
cmake .. -G Ninja
I want to:
cd myrepo/subfolder
mkdir -p build && pushd build
cmake .. -G Ninja
The difference is that I want to make use of a CMake project in a subfolder instead of the root folder. I have been unable to build anything but the root project.
I've tried:
Right-click CMakeLists.txt and Configure CMake - it configures the top-level CMake project instead of the file I am clicking on.
Right-click CMakePresets.json and click all the "Add..." etc. there. It seems to edit the presets file itself.
I could not figure out how to have many CMake builds in one project cloned from a repo, but I figured out how to open any folder as its own CMake project:
File -> Open -> CMake...
Select the subfolder as your project. Then MSVC will trample your CMakePresets.json (truncating it to 0 bytes) which you can reset with Git -> Undo Changes. Once you have undone the changes you will want to reset the CMake cache and reconfigure in order to avoid failed CMake compilation tests.
And now it should work. Unfortunately it doesn't seem to be very easy to clone a repo and then work with $project inside it.
I am building my project with cmake. I am able to generate the Makefiles on linux platform and hence able to build the project as well.
However, When I want to clean the all generated files, I am not able to do so.
I am not able to remove the foo.c.gcno and foo.c.d kind of files, using make clean command.
Is there any specific command in cmake to remove these intermediate files ?
Note: I have different directories for Cmake-source and build. I am concerned about the generated files(*.c.gcno and *.d) in the build directory. Rest all other files like *.o and *.so are cleaned up by make clean command.
In CMake, you cannot generate a Makefile and delete the rest. All the generated files, and an installed CMake, is still needed to build the project.
You should have run cmake <source directory> in a different directory in the first place. Let's call it build directory.
Then, build directory can be completely removed without changing the source code.
I'm working on my first project using cmake, and for the most part it's been going well but I've run into one problem I can't figure out.
Let's say I have my CMakeLists.txt file located at ~/project/build. I would like for the output from cmake (not the binaries, but the makefile/configuration files) to be independent of where I run cmake from.
As an example, if my terminal is sitting in the ~/project/build directory, calling cmake ~/project/build creates the makefile and everything else within the ~/project/build directory. This is the behaviour that I'd like. If I call cmake ~/project/build from anywhere else, it creates the makefile and everything else in whatever directory the terminal called the program from.
Is it possible to force cmake to generate its makefile and associated files in the same folder as the CMakeLists.txt file? I've taken a look through the documentation and I've had no problems figuring out how to change binary output directories, but I can't really find any mention of what I'm trying to do.
I realize this is a pretty minor annoyance (it's not that hard to move into my build folder before building the project) but I'm just wondering if it's possible and if there's some reason it wouldn't be advised.
You have to use 2 commands for this
1) cmake -B "Dest path(Any path in which u want to generate the output files)" -H"Source path(root CMakeLists.txt path)"
2) cmake --build "Dest path"
This is what I have, when I started generation:
iMac:IXCSoftswitch alex$ /usr/bin/cmake -G Xcode .
-- CMAKE_SOURCE_DIR = /Users/alex/Desktop/ixc-v/IXCSoftswitch, CMAKE_BINARY_DIR = /Users/alex/Desktop/ixc-v/IXCSoftswitch
CMake Error at CMakeLists.txt:25 (MESSAGE):
Binary and source directory cannot be the same
-- Configuring incomplete, errors occurred!
How can I fix it? I'm new to CMake, samples appreciated.
CMake is used to produce out-of-source builds.
The idea here is that you don't mix the files created during compilation with the original source files. In practice, you usually run CMake from a new, empty build directory and give the path to the source directory as an argument.
cd IXCSoftswitch
mkdir build
cd build
cmake -G Xcode ..
All of the files generated by CMake (which could be a lot) will now go into the build subdirectory, while your source directory stays clean of build artifacts.
The concept of out-of-source builds may seem strange at first, but it is actually a very convenient way of working once you get used to it.
From the root of your project directory.
cmake -G Xcode -H. -B_build
This is similar to the answer above. However, you are manually managing the out of source build. -B sets your target build directory (I happen to prefer _build). I tried looking up -H to double check, but couldn't find it. If memory serves, it specifies where your CMakeLists.txt lives.
I keep this command in a .sh/.bat file (depending). This way, I can keep my scripts that build my project in the root where a new person can easily find them.
I have a project organized as follows:
include/
src/
share/myprogram/
where share/myprogram/ contains resources.
My program is accessing these resources using relative paths. The executable expects to find them in ../share/myprogram/.
I would like when I run:
mkdir build
cd build
cmake ..
make
to have the following to happen:
make a bin directory
compile and put the executable in bin/
copy the share directory in the build directory
I am looking for a clean way of doing this. Ideally, I would like CMake to be aware of the resources as resources.
I know that I could use a copy custom command. Is this the only way to achieve this?
Bonus
If the resources could appear under Resources in Xcode when using the Xcode generator, and the copy be a clean copy phase under the mybin target, that would be awesome (and that's what I mean by CMake being aware of the resources as resources.)
Update:
What I have thus far:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
add_executable(mybin ${Headers} ${Sources})
add_custom_target(
Resources ALL
${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/share ${PROJECT_BINARY_DIR}/share
SOURCES ${Resources}
)
You may use Configure_file for copying files from source dir to binary dir. it has parameter Copyonly.
I am doing a unix command line tool, I am not doing a Mac OS X bundle and Xcode's resource folder is only for bundle resources so forget the bonus.
I realized that I wasn't doing things correctly.
Using a relative path to access a resource is not reliable since the program can be executed from anywhere.
What I did is to look for resources in a hierarchy of folder, starting from user specified, to environment variable, to relative directories and finally to standard unix directories.
So actually, the copy phase during the build is not necessary anymore. Only the installation matters and that is fairly easy:
install(DIRECTORY share/ DESTINATION share)
FYI, I kept my custom_target as is since I like having resources visible in Xcode, and calling it Resources makes it pretty :)