1

My .vcxproj file had this segment, which includes x64/Debug and x64/Release for Configuration and Platform. I am working on Windows machine

  <ItemGroup Label="ProjectConfigurations">
    <ProjectConfiguration Include="Debug|x64">
      <Configuration>Debug</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
    <ProjectConfiguration Include="Release|x64">
      <Configuration>Release</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>
..........
      </ItemGroup>

I now tried to create CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(LogMsg)

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

# Add executable target
add_executable(LogMsg ${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>"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_BINARY_DIR}/MSG00001.bin" "$<TARGET_FILE_DIR:LogMsg>"
)

After that I run cmake command: Tried two approaches:

cmake -S . -B . -G "Visual Studio 17 2022" 
cmake -S . -B . -G "Visual Studio 17 2022" -DCMAKE_BUILD_TYPE=Debug

But in both cases I am not getting x64/Debug and Release folders with my .lib file.

Can someone take a look what I am missing in configuration of the CMakeLists?

UPDATE My new CMakeLists.txt file is the following:

cmake_minimum_required(VERSION 3.5)

project(FALogMsg)

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

# Add executable target
add_library(FALogMsg ${SOURCES})

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

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

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

Error:

C:\Projects..\base\LogMsg>cmake --build build --config Release MSBuild version 17.6.3+07e294721 for .NET Framework

Building Custom Rule C:/Projects/../base/LogMsg/CMakeLists.txt
LogMsg.cpp LogMsg.vcxproj -> C:\Projects\AccurevMigration\Projects\workspace\OTM_PlaceHolder_Work_v2\base\LogMsg\build\Release\LogMsg.lib Error copying file (if different) from "C:/Projects/AccurevMigration/Projects/workspace/OTM_PlaceHolder_Work_v2/base/LogMsg/build/MSG00001.bin" to "C:/Projects/AccurevMigration/Projects/workspace/OTM_PlaceH
older_Work_v2/base/LogMsg/build/Release". C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: The command "setlocal [C:\Projects\AccurevMigration\Projects\workspace\OTM _PlaceHolder_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: "C:\Program Files\CMake\bin\cmake.exe" -E copy_if_different C:/Projects/Ac curevMigration/Projects/workspace/OTM_PlaceHolder_Work_v2/base/LogMsg/LogMsg.rc C:/Projects/AccurevMigration/Projects/workspace/OTM_PlaceHolder_Work_v2/base/LogMsg/build/Release [C:\Projects\AccurevMigrat ion\Projects\workspace\OTM_PlaceHolder_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: if %errorlevel% neq 0 goto :cmEnd [C:\Projects\AccurevMigration\Projects\w orkspace\OTM_PlaceHolder_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: "C:\Program Files\CMake\bin\cmake.exe" -E copy_if_different C:/Projects/Ac curevMigration/Projects/workspace/OTM_PlaceHolder_Work_v2/base/LogMsg/LogMsg.h C:/Projects/AccurevMigration/Projects/workspace/OTM_PlaceHolder_Work_v2/base/LogMsg/build/Release [C:\Projects\AccurevMigrati on\Projects\workspace\OTM_PlaceHolder_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: if %errorlevel% neq 0 goto :cmEnd [C:\Projects\AccurevMigration\Projects\w orkspace\OTM_PlaceHolder_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: "C:\Program Files\CMake\bin\cmake.exe" -E copy_if_different C:/Projects/Ac curevMigration/Projects/workspace/OTM_PlaceHolder_Work_v2/base/LogMsg/build/MSG00001.bin C:/Projects/AccurevMigration/Projects/workspace/OTM_PlaceHolder_Work_v2/base/LogMsg/build/Release [C:\Projects\Accure vMigration\Projects\workspace\OTM_PlaceHolder_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: if %errorlevel% neq 0 goto :cmEnd [C:\Projects\AccurevMigration\Projects\w orkspace\OTM_PlaceHolder_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: :cmEnd [C:\Projects\AccurevMigration\Projects\workspace\OTM_PlaceHolder_Wo rk_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone [C:\Projects\Acc urevMigration\Projects\workspace\OTM_PlaceHolder_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: :cmErrorLevel [C:\Projects\AccurevMigration\Projects\workspace\OTM_PlaceHo lder_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: exit /b %1 [C:\Projects\AccurevMigration\Projects\workspace\OTM_PlaceHolde r_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: :cmDone [C:\Projects\AccurevMigration\Projects\workspace\OTM_PlaceHolder_W ork_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: if %errorlevel% neq 0 goto :VCEnd [C:\Projects\AccurevMigration\Projects\w orkspace\OTM_PlaceHolder_Work_v2\base\LogMsg\build\LogMsg.vcxproj] C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(160,5): error MSB3073: :VCEnd" exited with code 1. [C:\Projects\AccurevMigration\Projects\workspa ce\OTM_PlaceHolder_Work_v2\base\LogMsg\build\LogMsg.vcxproj]

vel
  • 1,000
  • 1
  • 13
  • 35
  • 1
    Is there any reason to force this specific folder(s)? IMHO this is bad practice. – Marek R May 23 '23 at 10:42
  • 1
    Note: for multi config generators like VS cmake automatically appends a directory matching the configuration name unless you're using a generator expression. Setting the `RUNTIME_OUTPUT_DIRECTORY` target property to `"${CMAKE_CURRENT_SOURCE_DIR}"` should be sufficient for that and also include `RelWithDebugInfo` and `MinSizeRel` configurations. – fabian May 23 '23 at 11:17
  • why would you expect a x64/Debug directory when you wrote `RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/Debug"`, which has no "x64" in it? – starball May 23 '23 at 15:36

1 Answers1

7

The visual studio cmake generator is a multi-config generator, i.e. you create project files that allow you to build multiple configurations. You pass the configuration to work with via --config <configuration name> for cmake --build, cmake --install, ect. instead of providing this info during the configuration.

(Overlapping source and binary dirs is not recommended btw, so I'll use the build subdir here)

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

Note that in general it's not a good idea to create files in the source tree, since this results in issues when trying to set up multiple configurations based on the same source tree. Writing to be binary directories is recommended. Furthermore you didn't specify anything about x64 in the target properties at all. CMake won't insert this automatically (; It can't even generate projects supporting both x64 and Win32 configs).

Also for executable targets cmake won't generate .lib files, but .exe files...

fabian
  • 80,457
  • 12
  • 86
  • 114
  • thank you for your assistance. My 1st command pass, but then I face issue for generated EXE file when trying to run Release of Debug configuration. `LINK : error LNK2001: unresolved external symbol mainCRTStartup [C:\..\base\LogMsg\build\LogMsg.vcxproj] C:\..\base\LogMsg\x64\Release\LogMsg.exe : fatal error LNK1120: 1 unresolved externals [C:\..\base\LogMsg\build\LogMsg.vcxproj]` Do you know what might be wrong how to resolve this? – vel May 23 '23 at 13:21
  • ***Do you know what might be wrong how to resolve this?*** It looks like `SOURCES` does not have a source cpp file containing an `int main()`. You need an `int main()` to create an application and at least 1 source file. – drescherjm May 23 '23 at 13:55
  • @drescherjm - thanks for your envolvment. So you would expect for some .cpp file to have this main method, correct? – vel May 23 '23 at 21:21
  • Yes, like almost all c++ programs. For a c++ compiler to make a program you need at least 1 source file which is not a header and all c++ programs require a startup function: [https://en.cppreference.com/w/cpp/language/main_function](https://en.cppreference.com/w/cpp/language/main_function) – drescherjm May 23 '23 at 21:37
  • Thank you. This is not my app but I am DevOps and needs to build it. They do have file `LogMsg.cpp` which I added to SOURCES. Its definition is the following `BOOL APIENTRY DllMain( HANDLE /*hModule*/, DWORD /*ul_reason_for_call*/, PVOID /*pReserved*/ ) { return TRUE; } // DllMain` But I am getting new error once I add this file to SOURCES. `MSVCRT.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl __scrt_common_main_seh(void)" (?__scrt_common_main_seh@@YAHXZ)` and `fatal error LNK1120: 1 unresolved externals` – vel May 23 '23 at 21:49
  • @drescherjm what is confusing me is the fact that build using the `msbuild` command pass `msbuild %PRJ% /p:Configuration=%DBGREL% /p:platform=%PLATFORM%` for the same .vcxproj file while `cmake` is failing for not so sure about the reason.. if you have any tips that might help me it would mean a lot.. thx – vel May 23 '23 at 21:51
  • If you have a DLLMain you wanted `add_libraray(LogMsg SHARED ...)` and not `add_executable(LogMsg ...)` – drescherjm May 23 '23 at 22:00
  • @drescherjm you are a true guru! It seems things are moving. I added `add_library(FALogMsg ${SOURCES})` My `copy_if_different` is failing now for MSG00001.bin. I am not sure if this is needed. I have updated my Question with new CMakeLists.txt file and the thrown Error if you can take a quick look – vel May 23 '23 at 22:17
  • @drescherjm could you please assist me with the following question as well? https://stackoverflow.com/questions/76319278/cmake-how-to-get-all-outputs-in-release-subfolder – vel May 24 '23 at 00:00
  • 1
    I think you will have to ask a new question about that. We are way off-topic on this answer and question and there is not enough information to help. – drescherjm May 24 '23 at 00:32