首页 > 解决方案 > 通过 CMake 设置 Ninja 值(交叉编译)

问题描述

我目前正在尝试将我在 macOS 上编写的程序交叉编译到 Linux,并且我必须对build.ninja文件进行一些更改以使其成为可能(特别是链接器命令/链接库的其他位置)。但是,当我重新配置 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 文件成功编译,我也会收到此输出消息,并且代码无法在 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

这是我得到的输出消息:

[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 输出:

[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.

标签: c++linuxcmakecross-compilingninja

解决方案


经过一周的努力,我终于找到了解决方案。解决方案来自这个答案。这是我最终可以为 MacOS 和 Linux 编译的 CMake 文件:

# 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 上传递的参数:

-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

推荐阅读