[英]Building a C++ project with CMake on OSx: ld: symbol(s) not found for architecture x86_64
I got the source code of an old C++ project using Xerces-C++ that I am trying to build on CLion with CMake.我得到了一个使用 Xerces-C++ 的旧 C++ 项目的源代码,我正在尝试使用 CMake 在 CLion 上构建该项目。 OSx version: Catalina.
OSx 版本:Catalina。
I started by preparing the CMakeLists.txt that was not available on the old project.我首先准备了旧项目中没有的 CMakeLists.txt。
I arrived at this build exception:我遇到了这个构建异常:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug --target all -- -j 4 VERBOSE=1
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -S/Users/miloscuculovic/CLionProjects/Test2 -B/Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_progress_start /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug/CMakeFiles /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug/CMakeFiles/progress.marks
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/Makefile2 all
/Library/Developer/CommandLineTools/usr/bin/make -f src/CMakeFiles/Test2.dir/build.make src/CMakeFiles/Test2.dir/depend
cd /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug && /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_depends "Unix Makefiles" /Users/miloscuculovic/CLionProjects/Test2 /Users/miloscuculovic/CLionProjects/Test2/src /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug/src /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug/src/CMakeFiles/Test2.dir/DependInfo.cmake --color=
/Library/Developer/CommandLineTools/usr/bin/make -f src/CMakeFiles/Test2.dir/build.make src/CMakeFiles/Test2.dir/build
[ 50%] Linking CXX executable Test2
cd /Users/miloscuculovic/CLionProjects/Test2/cmake-build-debug/src && /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_link_script CMakeFiles/Test2.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/c++ -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/Test2.dir/ComputeDelta.cpp.o -o Test2 -framework CoreFoundation -framework CoreFoundation -framework IOKit /usr/lib/libobjc.dylib /usr/lib/libcurl.dylib
Undefined symbols for architecture x86_64:
"xercesc_3_2::XMLAttDefList::serialize(xercesc_3_2::XSerializeEngine&)", referenced from:
vtable for xercesc_3_2::XMLAttDefList in ComputeDelta.cpp.o
"NodesManager::PrintStats()", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::FullBottomUp(int)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::topdownMatch(int, int)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::setUniqueIdHandler(UniqueIdHandler*)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::registerResultDocument(XID_DOMDocument*)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::registerSourceDocument(XID_DOMDocument*)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::Optimize(int)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::MatchById(int)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::NodesManager()", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"NodesManager::~NodesManager()", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"DeltaConstructor::getDeltaDocument()", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"DeltaConstructor::constructDeltaDocument()", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"DeltaConstructor::DeltaConstructor(NodesManager*, char const*, XID_DOMDocument*, char const*, XID_DOMDocument*, bool)", referenced from:
XidXyDiff(XID_DOMDocument*, char const*, XID_DOMDocument*, char const*, bool, bool) in ComputeDelta.cpp.o
"xercesc_3_2::XMLAttDefList::getProtoType() const", referenced from:
vtable for xercesc_3_2::XMLAttDefList in ComputeDelta.cpp.o
"xercesc_3_2::XMLAttDefList::isSerializable() const", referenced from:
vtable for xercesc_3_2::XMLAttDefList in ComputeDelta.cpp.o
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src/Test2] Error 1
make[1]: *** [src/CMakeFiles/Test2.dir/all] Error 2
make: *** [all] Error 2
There are two CMakeLists.txt files, one in the root of the project and the 2nd one in src:有两个 CMakeLists.txt 文件,一个在项目的根目录,第二个在 src:
CMakeLists.txt at root:根目录下的 CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(Test2)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(src)
CMakeLists.txt at src: src 中的 CMakeLists.txt:
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(/usr/local/Cellar/xerces-c/3.2.2/include)
include_directories(/usr/local/Cellar/opencascade/7.3.0p3/include)
include_directories(/usr/local/Cellar/libuv/1.31.0/include)
add_executable(Test2 ComputeDelta.cpp)
set(STLINK_LIB_SHARED ${PROJECT_NAME})
find_library(ObjC objc)
find_library(Curl curl)
find_library(CoreServices CoreServices)
find_library(CoreFoundation CoreFoundation)
find_library(IOKit IOKit)
target_link_libraries(${STLINK_LIB_SHARED} ${CoreServices} ${CoreFoundation} ${IOKit} ${ObjC} ${Curl})
Any idea how to solve this?知道如何解决这个问题吗? I already installed opencascade, libuv, libev.
我已经安装了 opencascade、libuv、libev。
Both libuv and xerces-c have a pkg-config ( .pc
) file, so you can reduce your CMakeLists.txt to the following. libuv 和 xerces-c 都有一个 pkg-config (
.pc
) 文件,因此您可以将 CMakeLists.txt 缩减为以下内容。 This makes use of the FindPkgConfig
module that converts a pkg-config file to an IMPORTED target (See "It's time to do CMake right" . This IMPORTED target will automatically know which headers to include and which libraries to link, you only need to use target_link_libraries
to link with it.这利用了将 pkg-config 文件转换为 IMPORTED 目标的
FindPkgConfig
模块(请参阅“是时候正确执行 CMake” 。这个 IMPORTED 目标将自动知道要包含哪些标头以及要链接哪些库,您只需要使用target_link_libraries
与它链接。
include(FindPkgConfig)
pkg_check_modules(Xerces REQUIRED IMPORTED_TARGET xerces-c)
pkg_check_modules(LibUv REQUIRED IMPORTED_TARGET libuv)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(/usr/local/Cellar/opencascade/7.3.0p3/include)
add_library(libxyDelta STATIC
convertUTF.cpp
StringPusher.cpp
ComputeDelta.cpp
Diff_DeltaConstructor.pp
Diff_NodesManager.cpp
Diff_UniqueIdHandler.cpp
DeltaApply.cpp
DeltaException.cpp
DeltaManager.cpp
DeltaReverse.cpp
DeltaSortOperations.cpp
easy_css.cpp
lcss.cpp
lookup2.cpp
Tools.cpp
XID_map.cpp
XID_DOMDocument.cpp
XyDeltaFileImpl.cpp
XyDeltaDomImpl.cpp
XyInt.cpp
XyLatinStr.cpp
XyStr.cpp
XyStrDiff.cpp
XyStrDelta.cpp
XyUTF8Str.cpp)
set(STLINK_LIB_SHARED ${PROJECT_NAME})
find_library(ObjC objc)
find_library(Curl curl)
find_library(CoreServices CoreServices)
find_library(CoreFoundation CoreFoundation)
find_library(IOKit IOKit)
target_link_libraries(libxyDelta ${CoreServices} ${CoreFoundation} ${IOKit} ${ObjC} ${Curl})
target_link_libraries(libxyDelta PkgConfig::LibUv PkgConfig::Xerces)
add_executable(xydiff execComputeDelta.cpp)
target_link_libraries(xydiff libxyDelta)
add_executable(xydelta execDeltaApply.cpp)
target_link_libraries(xydelta libxyDelta)
I also changed your add_executable
to add_library
.我还将您的
add_executable
更改为add_library
。 Together, this will eliminate any errors about xerces and main
.总之,这将消除有关 xerces 和
main
的任何错误。 That just leaves you with the errors about NodesManager
and DeltaConstructor
, but I think these are in a separate .cpp
file you forgot to add to the target.这只会给您留下有关
NodesManager
和DeltaConstructor
的错误,但我认为这些错误位于您忘记添加到目标的单独.cpp
文件中。
You can probably get rid of most of those find_libraries
calls as well, but I cannot do that without knowing what your source files look like.您可能也可以摆脱大多数
find_libraries
调用,但如果不知道您的源文件是什么样的,我就无法做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.