CMake installation project does not copy my executable to the specified folder

I am just starting with CMake. I have successfully configured the most minimal Hello, World! A C ++ application is possible for Visual Studio 2012 on Windows 7, but I have one more nasty thing that is not entirely true, and I cannot understand why :(

My folder structure:

[cmakeTest] - [build] - [source] - [helloWorld] - main.cpp - CMakeLists.txt - CMakeLists.txt 

My main.cpp file is simple:

 #include <iostream> int main() { std::cout << "Hello World!"; } 

source/CMakeLists.txt is:

 cmake_minimum_required (VERSION 3.0.1) # Specifies project name for Visual Studio solution. # Visual Studio projects will be made for each CMake target specified project(cmakeTesting) # Set the install directory set(CMAKE_INSTALL_PREFIX ${cmakeTesting_BINARY_DIR}/bin) # Generate organiser projects # Creates "CMakePredefinedTargets" folder with INSTALL and ZERO_CHECK set_property(GLOBAL PROPERTY USE_FOLDERS ON) # Queue up CMakeLists from subdirectories add_subdirectory(helloWorld) 

source/helloWorld/CMakeLists.txt is:

 # Set Properties->General->Configuration Type to Application (.exe) # Creates helloWorld.exe with the listed sources (main.cxx) # Adds sources to the Solution Explorer add_executable (helloWorld main.cpp) # Creates a folder called "executables" and adds target # project (helloWorld.vcproj) under it set_property(TARGET helloWorld PROPERTY FOLDER "executables") # Adds logic to INSTALL.vcproj to copy helloWorld.exe to dest dir install (TARGETS helloWorld RUNTIME DESTINATION ${PROJECT_BINARY_BIN}/bin) 

What works:

  • It creates a solution for a Visual Studio / project solution in the assembly directory

  • The project is assembled and launched in debug and release mode

  • It creates exe files in /build/helloWorld/Debug/ and /build/helloWorld/Release (which work)

What does not work:

  • Visual Studio reports that it copied the EXE file to /bin/helloWorld.exe , but does not have> :-(
 1>------ Build started: Project: ZERO_CHECK, Configuration: Release Win32 ------ 2>------ Build started: Project: ALL_BUILD, Configuration: Release Win32 ------ 2> Build all projects 3>------ Build started: Project: INSTALL, Configuration: Release Win32 ------ 3> -- Install configuration: "Release" 3> -- Up-to-date: /bin/helloWorld.exe ========== Build: 3 succeeded, 0 failed, 1 up-to-date, 0 skipped ========== 

I know this seems fussy, but I'm trying to make sure that I understand everything that happens before moving on to more complex things (PS I use the CMake client, not the command line).

+7
source share
1 answer

It probably just comes down to being a typo. On the last line of the source / helloWorld / CMakeLists.txt file, I think you meant PROJECT_BINARY_DIR , not PROJECT_BINARY_BIN ?

What happens here is that ${PROJECT_BINARY_BIN}/bin allowed in /bin (dereferencing an undefined line in CMake, unfortunately, does not generate a warning), and /bin is an absolute path. If your project is located on the C: drive, I think you will find that C: \ bin \ helloWorld.exe really exists: Visual Studio is not fooling you :-)

In addition, relative paths are usually specified on the install command so that the user can select the installation root. Likewise, it is not very convenient to hardcode CMAKE_INSTALL_PREFIX (at least without warning).

In this case, I would change the install command to:

 install (TARGETS helloWorld RUNTIME DESTINATION bin) 

and remove set(CMAKE_INSTALL_PREFIX ...) from source / CMakeLists.txt.

Say the root of your project is C: \ myProject, then from the Visual Studio command prompt, you can do the following:

 cd C:\myProject\build cmake -DCMAKE_INSTALL_PREFIX="C:\myProject\build" ..\source cmake --build . --config Release --target INSTALL bin\helloWorld.exe 
+11
source

Source: https://habr.com/ru/post/974406/


All Articles