在 Linux 上使用 CMake + Clang + MinGW 构建 libobjc2

Building libobjc2 with CMake + Clang + MinGW on Linux

我正在尝试将 libobjc2 从 Arch 交叉编译到 Windows。我使用以下命令使用 CMake 进行配置:

cmake .. -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_PROCESSOR=x86_64 -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_FLAGS=$CLANG_WINDOWS_XFLAGS -DCMAKE_CXX_FLAGS=$CLANG_WINDOWS_XFLAGS -DCMAKE_INSTALL_PREFIX:PATH=/opt/cross/windows/gnustep

CLANG_WINDOWS_XFLAGS 定义为:

export CLANG_WINDOWS_XFLAGS="-target x86_64-w64-mingw32 -nostdinc -isystem /usr/lib/clang/3.6.0/include -isystem /usr/x86_64-w64-mingw32/include -isystem /usr/x86_64-w64-mingw32/include/c++/5.1.0 -isystem /usr/x86_64-w64-mingw32/include/c++/5.1.0/backward -isystem /usr/x86_64-w64-mingw32/include/c++/5.1.0/x86_64-w64-mingw32"

运行make后,libobjc.dll产品构建成功,但失败:

Linking C shared library libobjc.dll
[ 39%] Built target objc
Scanning dependencies of target objcxx
[ 40%] Building CXX object CMakeFiles/objcxx.dir/objcxx_eh.cc.obj
Linking C shared library libobjcxx.dll
CMakeFiles/objcxx.dir/objects.a(objcxx_eh.cc.obj):(.text+0x24c): undefined reference to `object_getClass'
CMakeFiles/objcxx.dir/objects.a(objcxx_eh.cc.obj):(.text+0x261): undefined reference to `objc_getClass'
CMakeFiles/objcxx.dir/objects.a(objcxx_eh.cc.obj):(.text+0x314): undefined reference to `objc_getClass'
CMakeFiles/objcxx.dir/objects.a(objcxx_eh.cc.obj):(.text+0x32c): undefined reference to `objc_getClass'
CMakeFiles/objcxx.dir/objects.a(objcxx_eh.cc.obj):(.text+0x3ac): undefined reference to `class_getSuperclass'
collect2: error: ld returned 1 exit status
clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
CMakeFiles/objcxx.dir/build.make:87: recipe for target 'libobjcxx.dll' failed
make[2]: *** [libobjcxx.dll] Error 1
CMakeFiles/Makefile2:95: recipe for target 'CMakeFiles/objcxx.dir/all' failed
make[1]: *** [CMakeFiles/objcxx.dir/all] Error 2
Makefile:146: recipe for target 'all' failed
make: *** [all] Error 2

我似乎无法让 MinGW link libobjcxx.dlllibobjc.dll。非交叉编译库编译成功

任何关于如何让它工作的提示都将不胜感激。

好的,所以我想我需要将 -lobjc 添加到 CMAKE_CXX_FLAGS,但我从来没有想过要为库添加链接器搜索路径。 运行 make 之前的以下内容解决了我的问题:

cmake .. -DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_PROCESSOR=x86_64 -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_FLAGS=$CLANG_WINDOWS_XFLAGS -DCMAKE_CXX_FLAGS="$CLANG_WINDOWS_XFLAGS -L`pwd` -lobjc" -DCMAKE_INSTALL_PREFIX=/opt/cross/windows/gnustep