簡體   English   中英

通過 CMake 設置 Ninja 值(交叉編譯)

[英]Setting Ninja values through CMake (cross compilation)

我目前正在嘗試將我在 macOS 上編寫的程序交叉編譯為 Linux,我不得不對build.ninja文件進行一些更改以使其成為可能(特別是 linker 命令/鏈接庫的其他位置)。 但是,當我重新配置 CMake 時,這會被清除。 有沒有辦法可以使用 CMake 在我的 Ninja 文件中設置值,而不是從哪里設置它們? 我也在這個項目中使用 vcpkg 和 GCC 10.2.0 for x86_64 Linux。 忍者文件必須被修剪以適應字符限制,如果需要,我可以找到一種不同的方式來放置完整的文件。 這是我當前的 CMakeLists,第一個是我的頂級 CMakeLists,第二個是鏈接所有內容的 CMakeLists:

Removed to make room for edits, working file can be found in edit 2
# CMakeList.txt : CMake project for ImpromptuServer, include source and define
# project specific logic here.
#
cmake_minimum_required(VERSION 3.8)

add_subdirectory("database")
add_subdirectory("web")
add_subdirectory("api")
add_subdirectory("utils")

# Add source to this project's executable.
file(GLOB_RECURSE web ./web/*.cpp ./web/*.hpp)
file(GLOB_RECURSE database ./database/*.cpp ./datbase/*.hpp)
file(GLOB_RECURSE api ./api/*.cpp ./api/*.hpp)

add_library(libweb ${web})
add_library(libdata ${database})
add_library(libapi ${api})

add_executable(ImpromptuServer "ImpromptuServer.cpp" ${web} ${database} ${api})

set(ALL_LIBS Wt::Wt Wt::Dbo Wt::HTTP Wt::DboSqlite3 cryptopp-static json11 OpenSSL::SSL OpenSSL::Crypto cpr)

# TODO: Add tests and install targets if needed.
target_link_libraries(ImpromptuServer PRIVATE ${ALL_LIBS})
target_link_libraries(libweb PRIVATE Wt::Wt Wt::Dbo Wt::HTTP Wt::DboSqlite3)
target_link_libraries(libdata PRIVATE Wt::Wt Wt::Dbo Wt::HTTP Wt::DboSqlite3)
target_link_libraries(libapi PRIVATE ${ALL_LIBS})

我知道GLOB_RECURSE不是最佳實踐,但我不想更改它,除非它可以解決我的問題。 這是我原來build.ninja文件:

REMOVED due to irrelevancy to question.

這是我為開始工作而修改的一個:

REMOVED due to irrelevancy to question.

但是,即使我使用修改后的 ninja 文件成功編譯,我也會收到此 output 消息,並且代碼無法在 Linux 機器上運行:

/Volumes/CaseSensitive/cross/lib/gcc/x86_64-linux/10.2.0/../../../../x86_64-linux/bin/ld: warning: cannot find entry symbol arch_paths_first; defaulting to 0000000000414000

為 macOS 編譯並使用原始build.ninja文件時,代碼運行良好。

編輯 1

經過大量工作,我已經讓 CMake 使用工具鏈文件,但我仍然有一個揮之不去的問題 CMake 找不到Threads::Threads 這是我當前的CMakeLists.txt

Working file can be found in edit 2

這是我收到的 output 消息:

[main] Configuring folder: ImpromptuServer 
[cmake] -- Found OpenSSL: /usr/lib/libcrypto.dylib   
[cmake] -- Found ZLIB: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/lib/libz.tbd (found version "1.2.11") 
[cmake] -- Found CURL: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/lib/libcurl.tbd (found version "7.64.1")  
[cmake] -- Configuring done
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:331 (_add_executable):
[cmake]   Target "ImpromptuServer" links to target "Threads::Threads" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:20 (add_executable)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:331 (_add_executable):
[cmake]   Target "ImpromptuServer" links to target "Threads::Threads" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:20 (add_executable)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:331 (_add_executable):
[cmake]   Target "ImpromptuServer" links to target "Threads::Threads" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:20 (add_executable)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:331 (_add_executable):
[cmake]   Target "ImpromptuServer" links to target "Threads::Threads" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:20 (add_executable)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libapi" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:18 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libapi" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:18 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libapi" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:18 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libapi" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:18 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libdata" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:17 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libdata" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:17 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libdata" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:17 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libdata" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:17 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libweb" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:16 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libweb" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:16 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libweb" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:16 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libweb" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:16 (add_library)
[cmake] 
[cmake] 
[cmake] -- Generating done
[cmake] CMake Generate step failed.  Build files cannot be regenerated correctly.
[main] Configuring folder: ImpromptuServer 
[proc] Executing command: /usr/local/bin/cmake --no-warn-unused-cli -DCMAKE_TOOLCHAIN_FILE=/Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake -DCMAKE_CROSSCOMPILING=True -Wno-dev -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/Volumes/CaseSensitive/cross/bin/x86_64-linux-gcc -DCMAKE_CXX_COMPILER:FILEPATH=/Volumes/CaseSensitive/cross/bin/x86_64-linux-g++ -H/Users/jackhogan/Desktop/Code/C++/ImpromptuServer -B/Users/jackhogan/Desktop/Code/C++/ImpromptuServer/build -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] INFO Using Linux protocols
[cmake] -- Configuring done
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:331 (_add_executable):
[cmake]   Target "ImpromptuServer" links to target "Threads::Threads" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:20 (add_executable)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:331 (_add_executable):
[cmake]   Target "ImpromptuServer" links to target "Threads::Threads" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:20 (add_executable)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:331 (_add_executable):
[cmake]   Target "ImpromptuServer" links to target "Threads::Threads" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:20 (add_executable)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:331 (_add_executable):
[cmake]   Target "ImpromptuServer" links to target "Threads::Threads" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:20 (add_executable)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libapi" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:18 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libapi" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:18 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libapi" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:18 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libapi" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:18 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libdata" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:17 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libdata" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:17 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libdata" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:17 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libdata" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:17 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libweb" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:16 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libweb" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:16 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libweb" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:16 (add_library)
[cmake] 
[cmake] 
[cmake] CMake Error at /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake:360 (_add_library):
[cmake]   Target "libweb" links to target "Threads::Threads" but the target was not
[cmake]   found.  Perhaps a find_package() call is missing for an IMPORTED target, or
[cmake]   an ALIAS target is missing?
[cmake] Call Stack (most recent call first):
[cmake]   ImpromptuServer/CMakeLists.txt:16 (add_library)
[cmake] 
[cmake] 
[cmake] -- Generating done
[cmake] CMake Generate step failed.  Build files cannot be regenerated correctly.

編輯 2

現在一切都使用這個 CMake 文件正確配置:

# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required(VERSION 3.8)
project (ImpromptuServer C CXX)

if(${CMAKE_CXX_COMPILER_ID} STREQUAL AppleClang)
    #set(CMAKE_TOOLCHAIN_FILE /Users/jackhogan/Desktop/Code/Utilities/vcpkg/scripts/buildsystems/vcpkg.cmake)
    set(LIB_DIR /Users/jackhogan/Desktop/Code/Utilities/vcpkg/installed/x64-osx/debug/lib)
    add_compile_options(-std=c++17)
    find_package(threads REQUIRED)
else()
    message(INFO " Using Linux protocols")
    if(NOT ${CMAKE_TOOLCHAIN_FILE} STREQUAL /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake)
        message(FATAL_ERROR " Incorrect toolchain file")
    endif()

    add_compile_options(-std=gnu++17)

    #set(CMAKE_TOOLCHAIN_FILE /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake)
    set(CMAKE_CROSSCOMPILING true)
    set(CMAKE_SYSTEM_NAME Linux)
    set(CMAKE_SYSTEM_PROCESSOR x86_64)
    set(LIB_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/debug/lib)
    set(CMAKE_FIND_ROOT_PATH /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/debug/lib)
    set(CMAKE_SYSROOT /Volumes/CaseSensitive/cross/x86_64-linux/)

    # Find libraries, may be changed later
    set(wt_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/share/wt/)
    set(OPENSSL_INCLUDE_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include/openssl)
    set(OPENSSL_ROOT_DIR /Volumes/CaseSensitive/cross/x86_64-linux/lib/)
    set(cryptopp_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/share/cryptopp/)
    set(cpr_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/share/cpr/)

    # Find pthread
    set(CMAKE_THREAD_LIBS_INIT /Volumes/CaseSensitive/cross/x86_64-linux/lib/libpthread.a)

    include_directories(/Volumes/CaseSensitive/cross/x86_64-linux/lib/)
    include_directories(/Volumes/CaseSensitive/cross/x86_64-linux/lib64/)
    include_directories(/Volumes/CaseSensitive/cross/x86_64-linux/include/)
    include_directories(/Volumes/CaseSensitive/cross/x86_64-linux/include/linux/)
    include_directories(/Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/)
    #include_directories(/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include)

    #set(Boost_NO_SYSTEM_PATHS true)
    #set(BOOST_INCLUDEDIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include/boost/)
    #set(BOOST_LIBRARYDIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/debug/lib/)
    #add_link_options(-g -isysroot /Volumes/CaseSensitive/cross/x86_64-linux)
    find_package(boost REQUIRED)
    find_package(Threads REQUIRED)
endif()

# Install packages
find_package(wt REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(cryptopp CONFIG REQUIRED)
find_package(cpr CONFIG REQUIRED)

add_library(json11 STATIC IMPORTED)
set_target_properties(json11 PROPERTIES IMPORTED_LOCATION ${LIB_DIR}/libjson11.a)

# Include sub-projects.
add_subdirectory ("ImpromptuServer")

但是我收到很多這樣的錯誤:

[main] Building folder: ImpromptuServer 
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/jackhogan/Desktop/Code/C++/ImpromptuServer/build --config Debug --target all -- -j 22
[build] [22/29   3% :: 0.087] Building CXX object ImpromptuServer/CMakeFiles/libapi.dir/api/internal/ios/AppStore.cpp.o
[build] [23/29   6% :: 0.091] Building CXX object ImpromptuServer/CMakeFiles/ImpromptuServer.dir/api/internal/ios/AppStore.cpp.o
[build] [24/29  10% :: 1.946] Building CXX object ImpromptuServer/CMakeFiles/ImpromptuServer.dir/web/pages/About.cpp.o
[build] FAILED: ImpromptuServer/CMakeFiles/ImpromptuServer.dir/web/pages/About.cpp.o 
[build] /Volumes/CaseSensitive/cross/bin/x86_64-linux-g++ --sysroot=/Volumes/CaseSensitive/cross/x86_64-linux/ -DBOOST_ALL_NO_LIB -DBOOST_FILESYSTEM_DYN_LINK -DBOOST_THREAD_DYN_LINK -I/Volumes/CaseSensitive/cross/x86_64-linux/lib -I/Volumes/CaseSensitive/cross/x86_64-linux/lib64 -isystem /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include -isystem /usr/local/include -isystem /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include/openssl -isystem /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk -std=gnu++17 -MD -MT ImpromptuServer/CMakeFiles/ImpromptuServer.dir/web/pages/About.cpp.o -MF ImpromptuServer/CMakeFiles/ImpromptuServer.dir/web/pages/About.cpp.o.d -o ImpromptuServer/CMakeFiles/ImpromptuServer.dir/web/pages/About.cpp.o -c ../ImpromptuServer/web/pages/About.cpp
[build] In file included from /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/bits/localefwd.h:40,
[build]                  from /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/string:43,
[build]                  from /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/bitset:47,
[build]                  from /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include/Wt/WWebWidget.h:11,
[build]                  from /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include/Wt/WInteractWidget.h:10,
[build]                  from /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include/Wt/WContainerWidget.h:10,
[build]                  from ../ImpromptuServer/web/pages/About.hpp:3,
[build]                  from ../ImpromptuServer/web/pages/About.cpp:1:
[build] /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/x86_64-linux/bits/c++locale.h:52:23: error: 'uselocale' was not declared in this scope; did you mean 'setlocale'?
[build]    52 |   extern "C" __typeof(uselocale) __uselocale;
[build]       |                       ^~~~~~~~~
[build]       |                       setlocale
[build] /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/x86_64-linux/bits/c++locale.h:62:11: error: '__locale_t' does not name a type
[build]    62 |   typedef __locale_t  __c_locale;
[build]       |           ^~~~~~~~~~
[build] /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/x86_64-linux/bits/c++locale.h:69:26: error: '__c_locale' does not name a type
[build]    69 |   __convert_from_v(const __c_locale& __cloc __attribute__ ((__unused__)),
[build]       |                          ^~~~~~~~~~
[build] /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/x86_64-linux/bits/c++locale.h: In function 'int std::__convert_from_v(const int&, char*, int, const char*, ...)':
[build] /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/x86_64-linux/bits/c++locale.h:75:5: error: '__c_locale' was not declared in this scope; did you mean '__cloc'?
[build]    75 |     __c_locale __old = __gnu_cxx::__uselocale(__cloc);
[build]       |     ^~~~~~~~~~
[build]       |     __cloc
[build] /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/x86_64-linux/bits/c++locale.h:100:28: error: '__old' was not declared in this scope
[build]   100 |     __gnu_cxx::__uselocale(__old);
[build]       |                            ^~~~~
[build] /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/x86_64-linux/bits/c++locale.h:100:33: error: '__gnu_cxx::__uselocale' cannot be used as a function
[build]   100 |     __gnu_cxx::__uselocale(__old);
[build]       |                                 ^

編輯 3

這是我的 CMake output:

[main] Configuring folder: ImpromptuServer 
[driver] Removing /Users/jackhogan/Desktop/Code/C++/ImpromptuServer/build/CMakeCache.txt
[driver] Removing /Users/jackhogan/Desktop/Code/C++/ImpromptuServer/build/CMakeFiles
[proc] Executing command: /usr/local/bin/cmake --no-warn-unused-cli -DCMAKE_TOOLCHAIN_FILE=/Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake -DCMAKE_CROSSCOMPILING=True -Wno-dev -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/Volumes/CaseSensitive/cross/bin/x86_64-linux-gcc -DCMAKE_CXX_COMPILER:FILEPATH=/Volumes/CaseSensitive/cross/bin/x86_64-linux-g++ -H/Users/jackhogan/Desktop/Code/C++/ImpromptuServer -B/Users/jackhogan/Desktop/Code/C++/ImpromptuServer/build -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 10.2.0
[cmake] -- The CXX compiler identification is GNU 10.2.0
[cmake] -- Checking whether C compiler has -isysroot
[cmake] -- Checking whether C compiler has -isysroot - yes
[cmake] -- Checking whether C compiler supports OSX deployment target flag
[cmake] -- Checking whether C compiler supports OSX deployment target flag - no
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: /Volumes/CaseSensitive/cross/bin/x86_64-linux-gcc - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Checking whether CXX compiler has -isysroot
[cmake] -- Checking whether CXX compiler has -isysroot - yes
[cmake] -- Checking whether CXX compiler supports OSX deployment target flag
[cmake] -- Checking whether CXX compiler supports OSX deployment target flag - no
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: /Volumes/CaseSensitive/cross/bin/x86_64-linux-g++ - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] INFO Using Linux protocols
[cmake] -- Found Boost: /usr/local/lib/cmake/Boost-1.72.0/BoostConfig.cmake (found version "1.72.0")  
[cmake] -- Looking for pthread.h
[cmake] -- Looking for pthread.h - not found
[cmake] -- Found Threads: TRUE  
[cmake] -- Found OpenSSL: /usr/lib/libcrypto.dylib   
[cmake] -- Found ZLIB: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/lib/libz.tbd (found version "1.2.11") 
[cmake] -- Found CURL: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/lib/libcurl.tbd (found version "7.64.1")  
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: /Users/jackhogan/Desktop/Code/C++/ImpromptuServer/build
[cmakefileapi-parser] Code model version (2.1) of cmake-file-api is unexpected. Expecting (2.0). IntelliSense configuration may be incorrect.
[cmakefileapi-parser] Code model version (2.1) of cmake-file-api is unexpected. Expecting (2.0). IntelliSense configuration may be incorrect.

經過一周的努力,我終於找到了解決方案。 解決方案來自這個答案。 這是我的最終工作 CMake 文件,可以為 MacOS 和 Linux 編譯:

# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required(VERSION 3.8)
# Target-specific stuff to be done before creating the project
if(${USE_LINUX})
    # Prevents linker from not finding program entry point
    set(HAVE_FLAG_SEARCH_PATHS_FIRST 0)
endif()
project (ImpromptuServer CXX)

if(NOT ${USE_LINUX})
    message(INFO " Using MacOS protocols")
    if(NOT ${CMAKE_TOOLCHAIN_FILE} STREQUAL /Users/jackhogan/Desktop/Code/Utilities/vcpkg/scripts/buildsystems/vcpkg.cmake)
        message(FATAL_ERROR " Incorrect toolchain file")
    endif()
    set(LIB_DIR /Users/jackhogan/Desktop/Code/Utilities/vcpkg/installed/x64-osx/debug/lib)
    add_compile_options(-std=c++17)
    find_package(Threads REQUIRED)

else()
    message(INFO " Using Linux protocols")
    if(NOT ${CMAKE_TOOLCHAIN_FILE} STREQUAL /Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake)
        message(FATAL_ERROR " Incorrect toolchain file")
    endif()

    set(CMAKE_C_LINK_FLAGS "")
    set(CMAKE_CXX_LINK_FLAGS "")
    add_compile_options(-std=gnu++17)
    add_link_options(-Wl,--copy-dt-needed-entries)

    set(CMAKE_CROSSCOMPILING true)
    # Get rid of OSX SDK to prevent incorrect linking
    set(CMAKE_OSX_SYSROOT "/Volumes/CaseSensitive/cross/x86_64-linux/include/")

    set(LIB_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/debug/lib)
    set(CMAKE_FIND_ROOT_PATH /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/debug/lib)
    set(CMAKE_SYSROOT /)

    # Find libraries, may be changed later
    set(wt_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/share/wt/)
    set(cryptopp_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/share/cryptopp/)
    set(cpr_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/share/cpr/)

    set(OPENSSL_INCLUDE_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include/openssl)
    set(OPENSSL_ROOT_DIR /Volumes/CaseSensitive/cross/x86_64-linux/lib/)
    # Cheat a little bit by linking a dynamic library
    set(OPENSSL_CRYPTO_LIBRARY /Volumes/CaseSensitive/cross/x86_64-linux/lib/libcrypto.a)
    set(OPENSSL_USE_STATIC_LIBS true)

    # Special system library include directories
    set(ZLIB_INCLUDE_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include/)
    set(ZLIB_ROOT /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/lib/)
    set(CURL_INCLUDE_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include/curl/)

    # Find pthread and link to non-apple
    set(CMAKE_THREAD_LIBS_INIT /Volumes/CaseSensitive/cross/x86_64-linux/lib/libpthread.a)

    # GCC includes
    include_directories(SYSTEM /Volumes/CaseSensitive/cross/lib/gcc/x86_64-linux/10.2.0/include/)
    # System includes
    include_directories(SYSTEM /Volumes/CaseSensitive/cross/x86_64-linux/include/c++/10.2.0/)

    #set(BOOST_INCLUDE_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/include/boost/)
    set(Boost_NO_BOOST_CMAKE on)
    set(Boost_USE_STATIC_LIBS true)
    set(Boost_LIBRARY_DIR /Volumes/CaseSensitive/vcpkgLinux/installed/x64-linux/lib/)
    find_package(Boost COMPONENTS program_options filesystem thread chrono date_time REQUIRED)
    find_package(Threads REQUIRED)
endif()

# Install packages
find_package(wt REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(cryptopp CONFIG REQUIRED)
find_package(cpr CONFIG REQUIRED)

add_library(json11 STATIC IMPORTED)
set_target_properties(json11 PROPERTIES IMPORTED_LOCATION ${LIB_DIR}/libjson11.a)

# Include sub-projects.
add_subdirectory ("ImpromptuServer")

雖然所有路徑都是系統特定的,但以后可以通過一些更高級的腳本來改進。 下面是我分別在 MacOS 和 Linux 上傳入的 arguments:

-DCMAKE_TOOLCHAIN_FILE=/Users/jackhogan/Desktop/Code/Utilities/vcpkg/scripts/buildsystems/vcpkg.cmake -DUSE_LINUX=false
-DCMAKE_TOOLCHAIN_FILE=/Volumes/CaseSensitive/vcpkgLinux/scripts/buildsystems/vcpkg.cmake -DUSE_LINUX=true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM