简体   繁体   English

通过 CMake 设置 Ninja 值(交叉编译)

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

I am currently trying to cross compile a program I wrote on macOS to Linux, and I have had to make some changes to the build.ninja file to make that possible (specifically the linker commands/other places where libraries are linked).我目前正在尝试将我在 macOS 上编写的程序交叉编译为 Linux,我不得不对build.ninja文件进行一些更改以使其成为可能(特别是 linker 命令/链接库的其他位置)。 However, this gets wiped when I reconfigure CMake.但是,当我重新配置 CMake 时,这会被清除。 Is there a way that I could use CMake to set the values in my Ninja file instead of wherever they are being set from?有没有办法可以使用 CMake 在我的 Ninja 文件中设置值,而不是从哪里设置它们? I am also using vcpkg for this project with GCC 10.2.0 for x86_64 Linux.我也在这个项目中使用 vcpkg 和 GCC 10.2.0 for x86_64 Linux。 The ninja files had to be trimmed to fit under the character limit, I can find a different way to put up the complete ones if needed.忍者文件必须被修剪以适应字符限制,如果需要,我可以找到一种不同的方式来放置完整的文件。 Here are my current CMakeLists, the first being my top level one and the second being the one that links everything:这是我当前的 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})

I know GLOB_RECURSE is not best practice, but I don't want to change it unless it will fix my problem.我知道GLOB_RECURSE不是最佳实践,但我不想更改它,除非它可以解决我的问题。 Here is my original build.ninja file:这是我原来build.ninja文件:

REMOVED due to irrelevancy to question.

Here is the one that I modified to get working:这是我为开始工作而修改的一个:

REMOVED due to irrelevancy to question.

However, even when I compile successfully with the modified ninja file, I get this output message and the code fails to run on a Linux machine:但是,即使我使用修改后的 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

The code runs fine when compiling for macOS and using the original build.ninja file.为 macOS 编译并使用原始build.ninja文件时,代码运行良好。

Edit 1编辑 1

After a lot of work, I have gotten CMake to use the toolchain file, but I still have a lingering problem where CMake can't find Threads::Threads .经过大量工作,我已经让 CMake 使用工具链文件,但我仍然有一个挥之不去的问题 CMake 找不到Threads::Threads Here is my current CMakeLists.txt :这是我当前的CMakeLists.txt

Working file can be found in edit 2

Here is the output message that I get:这是我收到的 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.

Edit 2编辑 2

Everything now configures correctly with this CMake file:现在一切都使用这个 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")

But I get a ton of errors like this:但是我收到很多这样的错误:

[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]       |                                 ^

Edit 3编辑 3

Here is my CMake output:这是我的 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.

After a week of trying to get this to work, I finally have a solution.经过一周的努力,我终于找到了解决方案。 The solution came from this answer.解决方案来自这个答案。 Here is my final working CMake file that can compile for both MacOS and Linux:这是我的最终工作 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")

While all of the paths are system-specific, this could be improved later with some more advanced scripting.虽然所有路径都是系统特定的,但以后可以通过一些更高级的脚本来改进。 Here are the arguments I pass in on MacOS and Linux respectively:下面是我分别在 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