[在OSx上使用CMake构建C ++项目:ld:架构x86_64找不到符号

问题描述 投票:0回答:1

我获得了使用Xerces-C ++的旧C ++项目的源代码,我正在尝试使用CMake在CLion上构建该源代码。 OSx版本:Catalina。

我首先准备了旧项目中不可用的CMakeLists.txt。

我到达此构建例外:

/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

有两个CMakeLists.txt文件,一个在项目的根目录中,第二个在src中:

CMakeLists.txt根目录:

cmake_minimum_required(VERSION 3.15)
project(Test2)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(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})

任何想法如何解决这个问题?我已经安装了opencascade,libuv,libev。

c++ macos cmake x86-64 clion
1个回答
0
投票

libuv和xerces-c都具有pkg-config(.pc)文件,因此您可以将CMakeLists.txt缩小为以下内容。这利用了FindPkgConfig模块,该模块将pkg-config文件转换为IMPORTED目标(请参见"It's time to do CMake right"。此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(Test2 SHARED 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})
target_link_libraries(${STLINK_LIB_SHARED} PkgConfig::LibUv PkgConfig::Xerces)

我也将您的add_executable更改为add_library。一起,这将消除有关xerces和main的任何错误。这只为您提供有关NodesManagerDeltaConstructor的错误,但我认为这些错误在一个单独的.cpp文件中,您忘记将其添加到目标中。

您可能也可以摆脱大多数find_libraries调用,但是我不知道源文件的样子就无法这样做。

© www.soinside.com 2019 - 2024. All rights reserved.