1

My CMakeLists.txt file for windows is the following.

cmake_minimum_required(VERSION 3.5)

project(LogMsg)

# Set the project source files
set(SOURCES
    LogMsg.cpp
    LogMsgMain.rc
    LogMsg.h
    resource.h
    stdafx.h
)

# Add executable target
add_library(LogMsg SHARED ${SOURCES})

# Set the configuration-specific outputs
set_target_properties(LogMsg PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/Debug"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/Release"
)

# Add include directories
target_include_directories(LogMsg PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

# Specify custom build commands
add_custom_command(TARGET LogMsg POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/LogMsg.rc" "$<TARGET_FILE_DIR:LogMsg>"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/LogMsg.h" "$<TARGET_FILE_DIR:LogMsg>"
)

I run:

cmake -G "Visual Studio 17 2022" -S . -B .
cmake --build . --config Release

Within the Release subfolder I am getting .dll file, .rc and .h file.

However, directory LogMsg.tlog and some other files such as: LogMsg.dll.recipe, LogMsg.obj and LogMsgMain.res are generated within the path LogMsg.dir/Release and I cannot find how is this defined since I would like it in the Release folder immediately.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
vel
  • 1,000
  • 1
  • 13
  • 35
  • 2
    CMake allows to redefine output directory only for certain build artefacts. Other build artefacts are generated in the build directory under specific subdirectories and there is no way to change that. Why do you want to produce `.obj` in your own directory? Why do you ever want to output binaries into source directory? – Tsyvarev May 24 '23 at 00:30

1 Answers1

1

Short answer:

you can use the following commands:

cmake -G "Visual Studio 17 2022" -S . -B ./Release
cmake --build ./Release --config Release

Long answer:

When you want to generate a project build-system, you can use the following command:

cmake [<options>] -S <path-to-source> -B <path-to-build>

In your command, the path-to-build is ".", this means that the target build location(path-to-build) is current location, so some files are generated in the current location. If you want to get all outputs in the Release folder, just use this command:

cmake -G "Visual Studio 17 2022" -S . -B ./Release

the output files will be generated in your target location(./Release).

When you want to build a project, you can use the following command:

cmake --build <dir> [<options>] [-- <build-tool-options>]

for your project, use this command:

cmake --build ./Release --config Release

the output files will be generated in your target location.

For more information, please refer to CMake Documentation.

Tom
  • 177
  • 7
  • Thank you! Can you please assist me with the following question as well? Thanks a lot https://stackoverflow.com/questions/76338106/cmake-how-to-produce-both-dll-and-lib-as-ouputs – vel May 26 '23 at 06:26
  • @vel I have added a comment under your new question. – Tom May 26 '23 at 06:29
  • please accept my apology - I have permuted the question - I am getting .lib but not getting .dll. please sorry – vel May 26 '23 at 09:00