In CMake Bibliothek zum build hinzufügen



  • Hallo liebe Community!

    Ich möchte eine selbstgeschriebene Bibliothek veröffentlichen. Es existiert ein CMakeList.txt, das über "add_library" diese Bibliothek bauen lässt. das möchte ich quasi mit veröffentlichen.

    Diese Bibliothek baut selbst aber auf 4 weiteren Fremd-Bibliotheken auf. Diese kommen leider nicht mit einem CMakeLists file. Es gibt aber fertige binaries und vorkompilierte Bibliotheken, die (per Fallunterscheidung) ausreichen würden. Ist es möglich diese .a Dateien zum CMake dazu zu linken, sodass diese "mitgebaut" werden?

    Aktuell sieht das CMake ungefähr so aus:

    cmake_minimum_required(VERSION 3.10)
    project(MyLib)
    
    if(NOT UNIX AND NOT APPLE OR CYGWIN)
        set(WIN32 TRUE)
    else(NOT APPLE)
        set(LINUX TRUE)
    endif()
    
    #compiler flags
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
    
    #link to other libraries with cmake
    
    include(${CMAKE_CURRENT_LIST_DIR}/MyLib/lib/lynarmath/CMakeLists.txt)
    include(${CMAKE_CURRENT_LIST_DIR}/MyLib/lib/tfhandler/CMakeLists.txt)
    
    #link to libraries with .a file only
    
    #build library    
    
    add_library(LynarMoon
            ${CMAKE_CURRENT_LIST_DIR}/MyLib/engine/MyLib.h
            ${CMAKE_CURRENT_LIST_DIR}/MyLib/engine/MyLib.cpp
    )
    

    Ich möchte jetzt meinetwegen:

    NotMyLib.a
    

    Zum prozess vom Bau von MyLib hinzufügen. Ich hoffe es ist klar, was ich machen möchte xD

    Danke im Voraus!





  • @lynarstudios sagte in In CMake Bibliothek zum build hinzufügen:

    Ich möchte jetzt meinetwegen:
    NotMyLib.a

    versuch mal:

    target_link_libraries(projektname NotMyLib.a)



  • Hey!

    Vielen Dank für deine Antwort. Leider funktioniert das aus mir unerklärlichen Gründen nicht:

    cmake_minimum_required(VERSION 3.10)
    project(TestProject)
    message("Building TestDir:")
    
    # specify architecture, set ARCHITECTURE to X86 or X64
    
    set(X86 "1")
    set(X64 "2")
    set(ARCHITECTURE X64)    
    
    message("Selected architecture: ${ARCHITECTURE}")
    
    if(NOT UNIX AND NOT APPLE OR CYGWIN)
        set(WIN32 TRUE)
    else(NOT APPLE)
        set(LINUX TRUE)
    endif()
    
    # specify OS
    
    if(NOT UNIX AND NOT APPLE OR CYGWIN)
        set(WIN32 TRUE)
        message("Selected OS: Windows")
    else(NOT APPLE)
        set(LINUX TRUE)
        message("Selected OS: Linux")
    endif()
    
    # compiler flags
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
    message("Building with flags: ${CMAKE_CXX_FLAGS}")    
    
    # sub directories
    
    add_subdirectory(TestDirTest/lib/googletest-1.8.1)    
    
    #include directories
    
    if(LINUX)
        include_directories(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/dirhandler/linux)
    endif()
    
    if(WIN32)
        include_directories(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/dirhandler/windows)
    endif()
    
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/TestDirTest/lib/googletest-1.8.1/googletest/include)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/lynarmath/LynarMath)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/tfhandler/include)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/clock/include)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/SDL/src/SDL2/include)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/SDL/src/SDL2_image)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/SDL/src/SDL2_mixer)
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/SDL/src/SDL2_ttf)
    
    # include other CMakeLists.txt
    
    include(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/lynarmath/CMakeLists.txt)
    include(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/tfhandler/CMakeLists.txt)
    include(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/dirhandler/CMakeLists.txt)
    include(${CMAKE_CURRENT_SOURCE_DIR}/TestDir/lib/clock/CMakeLists.txt)
    
    # build executable
    
    add_executable(TestProject
        main.cpp
        ... hier stehen ganz viele andere Dateien
    )
    
    message("link TestDir libraries...")
    target_link_libraries(TestProject atomic lynarmath dirhandler tfhandler clock gtest gtest_main)
    
    message("link SDL libraries...")
    
    # x86
    
    if(${ARCHITECTURE} STREQUAL ${X86})
        message("\tSDL x86...")
    
        if(MINGW)
            message("\t\tlinking MinGW...")    
            link_directories(${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/MinGW/x86)
    
            target_link_libraries(TestProject SDL2.a)
            target_link_libraries(TestProject SDL2main.a)
            target_link_libraries(TestProject SDL2_image.a)
            target_link_libraries(TestProject SDL2_mixer.a)
            target_link_libraries(TestProject SDL2_ttf.a)
        endif()
    
        if(MSVC)
            message("\t\tlinking Visual Studio...")    
            link_directories(${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/VisualStudio/x86)
    
            target_link_libraries(TestProject SDL2.lib)
            target_link_libraries(TestProject SDL2main.lib)
            target_link_libraries(TestProject SDL2_image.lib)
            target_link_libraries(TestProject SDL2_mixer.lib)
            target_link_libraries(TestProject SDL2_ttf.lib)
        endif()
    else()
    
        message("\tx86 skipped!")
    
    endif()
        
    # x64
           
    if(${ARCHITECTURE} STREQUAL ${X64})
        message("\tSDL x64...")
    
        if(MINGW)
    
            message("\t\tlinking MinGW...")
            link_directories(${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/MinGW/x64)
    
            target_link_libraries(TestProject SDL2.a)
            target_link_libraries(TestProject SDL2main.a)
            target_link_libraries(TestProject SDL2_image.a)
            target_link_libraries(TestProject SDL2_mixer.a)
            target_link_libraries(TestProject SDL2_ttf.a)
        endif()
    
        if(MSVC)
            message("\t\tlinking Visual Studio...")    
            link_directories(${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/VisualStudio/x64)
    
            target_link_libraries(TestProject SDL2.lib)
            target_link_libraries(TestProject SDL2main.lib)
            target_link_libraries(TestProject SDL2_image.lib)
            target_link_libraries(TestProject SDL2_mixer.lib)
            target_link_libraries(TestProject SDL2_ttf.lib)
        endif()
    else()
        message("\tx64 skipped!")
    endif()
    
    if(LINUX)
        message("\t\tlinking Linux...")
        target_link_libraries(TestProject SDL2 SDL2main SDL2_image SDL2_mixer SDL2_ttf)
    endif()
    

    Und hier ist der CMake Konsolen Output:

    Building TestProject:    
    Selected architecture: X64    
    Selected OS: Windows    
    Building with flags:  -std=gnu++0x    
    -- Could NOT find PythonInterp (missing: PYTHON_EXECUTABLE)     
    link LynarMoon libraries...    
    link SDL libraries...    
    	x86 skipped!   
    	SDL x64...    
    		linking MinGW...    
    -- Configuring done    
    -- Generating done    
    -- Build files have been written to: C:/Users/user/CLionProjects/testproject/cmake-build-debug   
    
    [Finished]
    

    Die Dateien im add_executable() Befehl werden alle gefunden. Die Bibliothekdateien leider nicht. Ich habe auch schon versucht .a zu entfernen aus dem Namen. Leider ohne Erfolg. Ich habe auch kurz versucht find_path() zu benutzen, wie etwa:

    find_path(SDL_PATH ${CMAKE_CURRENT_LIST_DIR}/TestProject/lib/SDL/bin_lib/MinGW/x64/SDL2.a)
    

    Dieser Pfad wird scheinbar nicht gefunden. Allerdings findet er gar keinen Pfad mit find_path. Selbst nicht den Pfad der Dateien, die er vorher bereits erfolgreich kompiliert hat... Mir gehen langsam leider die Ideen aus...



  • Was übrigens auch sehr merkwürdig ist:

    if(MINGW)    
            if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/MinGW/x64/libSDL2.a)    
                message("\t\tlinking MinGW...")    
            else()    
                message("\t\tcouldn't link MinGW...")    
            endif()   
    
            find_path(SDL_FIND_PATH ${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/MinGW/x64/libSDL2.a)    
            message("\t\t${SDL_FIND_PATH}")    
            find_file(SDL_FIND_FILE ${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/MinGW/x64/libSDL2.a)    
            message("\t\t${SDL_FIND_FILE}")    
    
            link_directories(${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/MinGW/x64)    
            target_link_libraries(TestProject libSDL2.a)    
            target_link_libraries(TestProject libSDL2main.a)    
            target_link_libraries(TestProject libSDL2_image.a)    
            target_link_libraries(TestProject libSDL2_mixer.a)    
            target_link_libraries(TestProject libSDL2_ttf.a)    
        endif()
    

    nur das if Statement scheint zu funktionieren...



  • Hey!

    Das hier scheint bei mir nun zu funktionieren. Ich verzichte ganz auf link_directories(), obwohl mich echt interessieren würde, warum das nicht funktioniert. Hier meine Lösung:

    ######################################################    
    # set SDL paths    
    ######################################################    
    
    set(SDL_MINGW64_PATH ${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/MinGW/x64)    
    set(SDL_MINGW32_PATH ${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/MinGW/x86)    
    set(SDL_VC64_PATH ${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/VisualStudio/x64)    
    set(SDL_VC32_PATH ${CMAKE_CURRENT_LIST_DIR}/TestDir/lib/SDL/bin_lib/VisualStudio/x86)
    
    ...
    
    if(${ARCHITECTURE} STREQUAL ${X64})
        message("\tSDL x64...")
    
        if(MINGW)
            message("\t\tlinking MinGW...")
    
            target_link_libraries(${TARGET} PUBLIC "${SDL_MINGW64_PATH}/libSDL2.dll.a")
            target_link_libraries(${TARGET} PUBLIC "${SDL_MINGW64_PATH}/libSDL2main.a")
            target_link_libraries(${TARGET} PUBLIC "${SDL_MINGW64_PATH}/libSDL2_image.dll.a")
            target_link_libraries(${TARGET} PUBLIC "${SDL_MINGW64_PATH}/libSDL2_mixer.dll.a")
            target_link_libraries(${TARGET} PUBLIC "${SDL_MINGW64_PATH}/libSDL2_ttf.dll.a")
        endif()
    
        if(MSVC)
            message("\t\tlinking Visual Studio...")
    
            target_link_libraries(${TARGET} PUBLIC "${SDL_VC64_PATH}/SDL2.lib")
            target_link_libraries(${TARGET} PUBLIC "${SDL_VC64_PATH}/SDL2main.lib")
            target_link_libraries(${TARGET} PUBLIC "${SDL_VC64_PATH}/SDL2_image.lib")
            target_link_libraries(${TARGET} PUBLIC "${SDL_VC64_PATH}/SDL2_mixer.lib")
            target_link_libraries(${TARGET} PUBLIC "${SDL_VC64_PATH}/SDL2_ttf.lib")
        endif()
    else()
        message("\tx64 skipped!")
    endif()
    

    Funktioniert einwandfrei 🙂