How do I set the build path in cmake? - visual-studio-2010

I am a total newb to cmake, I am kind of overwhelmed at the moment.
The library I am using creates a visual studio project file using cmake. I would like to edit the cmake file so that it changes the "Output Directory" of the visual studio project to "../../../build/$(Configuration)/". I have no idea how to do this though.

Try adding these lines to your CMakeLists.txt file:
set(dir ${CMAKE_CURRENT_SOURCE_DIR}/../../build)
set(EXECUTABLE_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE)
set(LIBRARY_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE)

I ran into the same problem and ended up using CMAKE_RUNTIME_OUTPUT_DIRECTORY to set it. For your case it would be:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../build)

Related

CMake in Visual Studio does not set CMAKE_BINARY_DIR correctly

I have a CMake project which I am building in Visual Studio. In my CMakeLists.txt file, I have this statement:
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "Prevented in-tree build. ")
endif()
In my CMakeSettings.json I have this line:
{
"configurations":[
{
"name": "x64-Debug",
//...
"buildRoot": "{projectDir}\\out\\build\\${name}",
}
//...
]
//....
}
According to this documentation buildRoot maps to -DCMAKE_BINARY_DIR.
I get the error that you can see in the above if statement.
If I print the CMAKE_BINARY_DIR variable, it looks nothing like buildRoot, it's just the root of the project.
It did work previously, but I used CMake in the Developer Command Prompt for VS 2019 to try to build the project, but now, if I try to build again from Visual Studio, I get the above error (in-tree build).
Any idea what's wrong and how to properly set CMAKE_BINARY_DIR in CMakeSettings.json?
Solved the problem by deleting the CMakeCache.txt file. Turns out, I called CMake from the same directory as the project, and it created a CMakeCache.txt file containing the bad build directory.

Visual Studio and cmake errors when compiling openexr for windows

Apologies because I'm pretty new and inexperienced at this but I'm trying to compile OpenExr-2.3.0 (downloaded from http://www.openexr.com/downloads.html and extracted to a 'exr_2_3_0' directory) on Windows with cmake and Visual Studio 15. I'm just going to outline the basic questions I have before going into detail:
Should the ilmbase build do more than just create some header files in the relevant include directory? (I know the 2.2.0 build did and I've a feeling this is what's missing).
I'm confused as to how to tell the openexr build where these header files are. It never seems to be able to find them so I'm pretty sure I'm either building them to the wrong place or missing a step.
The README file suggests the following:
Launch a command window, navigate to the IlmBase folder with CMakeLists.txt,and type command: setlocal del /f CMakeCache.txt cmake
-DCMAKE_INSTALL_PREFIX= -G "Visual Studio 10 Win64" ..\ilmbase
Navigate to IlmBase folder in Windows Explorer, open ILMBase.sln and build the solution. When it build successfully, right click
INSTALL project and build. It will install the output to the path you
set up at the previous step.
Go to http://www.zlib.net and download zlib
Launch a command window, navigate to the OpenEXR folder with CMakeLists.txt, and type command: setlocal del /f CMakeCache.txt cmake
-DZLIB_ROOT= -DILMBASE_PACKAGE_PREFIX= -DCMAKE_INSTALL_PREFIX= -G "Visual Studio 10 Win64" ^ ..\openexr
Navigate to OpenEXR folder in Windows Explorer, open OpenEXR.sln and build the solution. When it build successfully, right click
INSTALL project and build. It will install the output to the path you
set up at the previous step.
So I create a deploy folder inside the source directory and from the source directory I run:
cd ilmbase-2.3.0
setlocal
del /f CMakeCache.txt
cmake -DCMAKE_INSTALL_PREFIX="../deploy" -G "Visual Studio 15 Win64" ../ilmbase-2.3.0
This builds a sln file in the ilmbase folder, which builds with no errors but the install project inside that solution only creates a bunch of header files inside the 'source/deploy/include/OpenEXR' folder. Again, is this all it's supposed to do or is there supposed to be a lib directory with dll files etc built as a result at this stage?
I've successfully built zlib with no errors but when I try point 4 I run into errors of:
CMake Error at IlmImfExamples/CMakeLists.txt:3 (ADD_EXECUTABLE) Target
"IlmImfExamples" links to target "IlmBase::IlmThread" but the target
was not found. Perhaps a find_package() call is missing for an
IMPORTED target or an ALIAS is missing?
I'm guessing I should be adding a path from cmake to the new files? Even if I open the openexr.sln that is generated and try to build, IlmImfExamples fails to build. If I manually add the header files that it complains about it still won't build, complaining that it can't find OpenEXR::IlmImf.lib It's right that this hasn't been built but I've no idea why, or at least I can't find it if it has.
I did see this thread on github:
https://github.com/openexr/openexr/issues/355
that says there are errors in the CMakeLists.txt which I have also changed. But I'm rapidly hitting a brick wall of knowledge at this stage so any help is greatly appreciated.
Thanks,
Paddy

cmake choose output directory .exe

I tried to define where to put the .exe generated by cmake + visual studio.
I put this in the cmakefiles.txt
IF(CMAKE_SYSTEM_NAME STREQUAL Windows)
INSTALL(TARGETS
mialsrtkRefineHRMaskByIntersection
DESTINATION ${CMAKE_INSTALL_PREFIX})
ENDIF(CMAKE_SYSTEM_NAME STREQUAL Windows)
but at the end all exe are in the folder of the vcxproj in a subfolder Debut.
is that normal ?
How can I specify the output directory ?
on linux I put destination bin and it works.
What you're setting is the installation destination directory. In Visual Studio, installation is performed by building the special target INSTALL. That is equivalent to make install in Make world.
Note that you can also specify the build output directory. The ways to do that are :
Variable CMAKE_RUNTIME_OUTPUT_DIRECTORY, which provides global settings.
Target property RUNTIME_OUTPUT_DIRECTORY, which controls settings for a particular target.
Both of these have per-configuration variants, or sister variables/properties which affect other things than executables (e.g. CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG). Please see the CMake docs for details.

How to refer to Visual Studio output directory in CMakeLists.txt?

I have a cross-platform project which uses CMake. I am doing out-of-source builds so basically I have a source directory "src" which contains the CMakeLists.txt and then I have a "src/build" directory where I generate the out-of-source build.
However when using Visual Studio (2013) it does not place the executable in the build dir but to "src/build/Debug" for example (depends on the selected configuration). I think this was referred as "$(OutDir)" inside VS. How do I refer to the (runtime) output directory in my CMakeLists.txt so I can copy DLLs and shaders where my executable is?
Edit: The suggested duplicate answer does not seem correct to me. In that answer you just force the output directories to be static which sounds wrong if you are using a multi-configuration build system like Visual Studio.
I solved my problem by copying the files with the "file (GENERATE OUTPUT)" which supports the cmake-generator-expressions suggested by StAlphonzo:
foreach (file ${SHADER_SOURCES})
file (GENERATE OUTPUT $<CONFIG>/${file} INPUT ${PROJECT_SOURCE_DIR}/${file})
endforeach()
I would also like to mention a second way of solving this problem I discovered that I think is actually more correct, by adding a custom command:
add_custom_command(TARGET target ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCES_DIR}/foo $<TARGET_FILE_DIR:target>)

How to add CMake INSTALL project automatically to build configurations

I'm trying to deploy a couple of .ini files to the binary directory when compiling, using CMake. I did this through the following lines (typed by hand; feel free to edit if you find a typo)
FILE(GLOB INI_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../misc/*.ini")
INSTALL(FILES ${INI_FILES} CONFIGURATIONS Debug DESTINATION ${CMAKE_BINARY_DIR}/bin/Debug)
INSTALL(FILES ${INI_FILES} CONFIGURATIONS Release DESTINATION ${CMAKE_BINARY_DIR}/bin/Release)
This properly generates an INSTALL project in my Visual Studio solution which works if 'compiled' manually. However, the project is not part of the Debug and Release build configurations of the Solution, meaning it won't be executed when compiling the solution. Is there a way to add the project manually to the build configurations in CMake?
Or is this not intended and I need to call CMake differently?
I'm Using CMake 3.0.2 with Visual Studio 2013.
I'm generating my Visual Studio solution like this:
"%VS120COMNTOOLS%..\..\VC\vcvarsall" amd64
cmake .. -G "Visual Studio 12 2013 Win64"
Yes, you can add a custom command to copy the .ini files before you build the project. Assuming your build target is named my_target, you can do something like this:
FILE(GLOB INI_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../misc/*.ini")
add_executable(my_target test.cpp)
foreach(FN ${INI_FILES})
add_custom_command(TARGET my_target
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "${FN}" "${CMAKE_CURRENT_BINARY_DIR}")
endforeach()
It basically uses cmake to copy the .ini files individually over to the build directory.

Resources