【发布时间】:2022-01-05 06:28:43
【问题描述】:
我有一个使用 CMake 作为其构建系统的 C++ 项目。在将它移植到 macOS 时,我需要集成几个 Objective-C++ 文件,但不幸的是,我得到的只是构建错误。我远不是 Objective-C++ 方面的专家,这也无济于事。
为了达到我现在的位置,我首先更新了纯 C++ 项目的 project 定义以包含 C++ 和 Objective-C++:
project(projectFOO LANGUAGES CXX OBJCXX)
之后,我将所有“.mm”Objective-C++ 源文件及其标头直接传递到对add_library 的调用中。
但是,当我重建 cmake 项目时,我得到一堵编译器错误,几乎没有错误消息如下所示:
(...)
In file included from /Users/ram/development/Foo/source/MyNSImage.mm:1:
/Users/ram/development/Foo/include/MyNSImage.h:22:5: error: unknown type name 'CGContextRef'
(...)
/Users/ram/development/Foo/source/MyNSImage.mm:7:81: error: use of undeclared identifier 'nil'
(...)
查看构建执行的编译器命令后,我注意到它调用 /usr/bin/c++ 同时传递了 -x objective-c++ -g 和 -std=gnu++11。
之后,我可以通过创建一个仅包含 Objective-C++ 文件的库来重现相同的错误,其 CMake 定义如下:
cmake_minimum_required(VERSION 3.16) # Introduces support for OBJC and OBJCXX. See https://cmake.org/cmake/help/v3.16/release/3.16.html
project(projectFOO LANGUAGES CXX OBJCXX)
# (..omit C++ code..)
if(APPLE)
# separate library created just to build the Objective-C++ code
add_library(foo_mac
include/MyNSImage.h
source/MyNSImage.mm
)
target_include_directories(foo_mac
PUBLIC
include
)
endif()
# (..omit more C++ code..)
# here's the C++ library
add_library(foo
${foo_INCLUDES}
${foo_HEADERS}
)
if(APPLE)
# when building on macOS, also link the Objective-C++ lib.
target_link_libraries(foo foo_mac)
endif()
使用-DCMAKE_VERBOSE_MAKEFILE 刷新CMake 项目并重建它后,这是foo_mac 的编译器命令:
(...)
cd /Users/ram/development/Foo/cmake-build-debug/Foo && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/Users/ram/development/Foo/include -x objective-c++ -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -fPIC -std=gnu++11 -o CMakeFiles/foo_mac.dir/source/MyNSImage.mm.o -c /Users/ram/development/Foo/source/MyNSImage.mm
(...)
有人知道我可能做错了什么吗?
【问题讨论】:
-
您能否用整个编译器调用更新帖子(通过 make VERBOSE=1)?看起来您缺少一些包含。
-
@NickolayOlshevsky 当然。显然 cmake 正在调用
$(snip long path)/XcodeDefault.xctoolchain/usr/bin/c++。我将继续用一个最小的工作示例来更新问题。
标签: c++ macos cmake objective-c++