Search code examples
cmakeclion

My libraries aren't going to the specified directory in cmake


I'm creating a library, and it works fine, but I've specified the libraries to be created on the directory "${CMAKE_CURRENT_SOURCE_DIR}/Build/MSVC/bin" but is being created on the build folder with the name lib{ProyectName}.lib (it should not have the lib prefix) this is my CMakeLists:

cmake_minimum_required(VERSION 3.26)



add_definitions(-DUNICODE -D_UNICODE)

project(AxoSerial-MSVC)
set(CMAKE_CXX_STANDARD 17)
add_library(AxoSerial-MSVC STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/AxoSerial.cpp)
target_include_directories(AxoSerial-MSVC PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
set_target_properties(AxoSerial-MSVC
        PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/Build/MSVC/bin"
        CMAKE_CXX_COMPILER "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.29.30133/bin/HostX64/x64/cl.exe"
        SUFFIX ".lib"
)

project(AxoSerial-MinGW)
set(CMAKE_CXX_STANDARD 17)
add_library(AxoSerial-MinGW STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/AxoSerial.cpp
        main.cpp)
target_include_directories(AxoSerial-MinGW PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
set_target_properties(AxoSerial-MinGW
        PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/Build/MSVC/bin"
        CMAKE_CXX_COMPILER g++
        SUFFIX ".lib"
)


project(main)
set(CMAKE_CXX_STANDARD 17)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE AxoSerial-MSVC)
target_include_directories(main PUBLIC ${CMAKE_SOURCE_DIR}/include)

and this is my directory tree:

C:.
│   CMakeLists.txt
│   main.cpp
│
├───.idea
│       .gitignore
│       .name
│       AxoSerial.iml
│       misc.xml
│       modules.xml
│       workspace.xml
│
├───Build
│   │   .ninja_deps
│   │   .ninja_log
│   │   build.ninja
│   │   CMakeCache.txt
│   │   cmake_install.cmake
│   │   libAxoSerial-MinGW.lib
│   │   libAxoSerial-MSVC.lib
│   │   main.exe
│   │
│   ├───.cmake
│   │   └───api
│   │       └───v1
│   │           ├───query
│   │           │       cache-v2
│   │           │       cmakeFiles-v1
│   │           │       codemodel-v2
│   │           │       toolchains-v1
│   │           │
│   │           └───reply
│   │                   cache-v2-41e28885b8099980ddf6.json
│   │                   cmakeFiles-v1-4dd9e73c28e719aace8b.json
│   │                   codemodel-v2-c699778468e6a4bb9941.json
│   │                   directory-.-Debug-d0094a50bb2071803777.json
│   │                   index-2023-11-09T02-34-15-0214.json
│   │                   target-AxoSerial-MinGW-Debug-a763cdb921b125cef898.json
│   │                   target-AxoSerial-MSVC-Debug-bb5afb7fe9924aefe8ab.json
│   │                   target-main-Debug-503dda660556d3767039.json
│   │                   toolchains-v1-af93b67b788c93364498.json
│   │
│   ├───CMakeFiles
│   │   │   clion-Debug-log.txt
│   │   │   clion-environment.txt
│   │   │   cmake.check_cache
│   │   │   CMakeConfigureLog.yaml
│   │   │   rules.ninja
│   │   │   TargetDirectories.txt
│   │   │
│   │   ├───3.26.4
│   │   │   │   CMakeCCompiler.cmake
│   │   │   │   CMakeCXXCompiler.cmake
│   │   │   │   CMakeDetermineCompilerABI_C.bin
│   │   │   │   CMakeDetermineCompilerABI_CXX.bin
│   │   │   │   CMakeRCCompiler.cmake
│   │   │   │   CMakeSystem.cmake
│   │   │   │
│   │   │   ├───CompilerIdC
│   │   │   │   │   a.exe
│   │   │   │   │   CMakeCCompilerId.c
│   │   │   │   │
│   │   │   │   └───tmp
│   │   │   └───CompilerIdCXX
│   │   │       │   a.exe
│   │   │       │   CMakeCXXCompilerId.cpp
│   │   │       │
│   │   │       └───tmp
│   │   ├───AxoSerial-MinGW.dir
│   │   │   │   main.cpp.obj
│   │   │   │
│   │   │   └───src
│   │   │           AxoSerial.cpp.obj
│   │   │
│   │   ├───AxoSerial-MSVC.dir
│   │   │   └───src
│   │   │           AxoSerial.cpp.obj
│   │   │
│   │   ├───main.dir
│   │   │       main.cpp.obj
│   │   │
│   │   └───pkgRedirects
│   ├───MinGW
│   │   └───bin
│   ├───MSVC
│   │   └───bin
│   └───Testing
│       └───Temporary
│               LastTest.log
│
├───include
│       AxoSerial.h
│
└───src
        AxoSerial.cpp

As seen in the code, I've tried :

RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/Build/MSVC/bin"

Solution

  • From your example, you're setting RUNTIME_OUTPUT_DIRECTORY hoping that your library will be dumped in it.

    A quick check of CMake's documentation page on RUNTIME_OUTPUT_DIRECTORY shows us that the variable is used to specify a path whose role is "Output directory in which to build RUNTIME target files."

    If you follow the link to RUNTIME, you'll stumble upon this definition:

    A runtime output artifact of a buildsystem target may be:

    • The executable file (e.g. .exe) of an executable target created by the add_executable() command.
    • On DLL platforms: the executable file (e.g. .dll) of a shared library target created by the add_library() command with the SHARED option.

    Knowing this, if you take a look at your example you'll notice that all your library targets were created as STATIC libraries instead of a SHARED library.

    (...)
    
    
    add_library(AxoSerial-MSVC STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/AxoSerial.cpp)
    
    (...)
    
    add_library(AxoSerial-MinGW STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/AxoSerial.cpp
            main.cpp)
    
    (...)
    

    Hope this helps.