简体   繁体   中英

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. OSx version: Catalina.

I started by preparing the CMakeLists.txt that was not available on the old project.

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 at root:

cmake_minimum_required(VERSION 3.15)
project(Test2)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(src)

CMakeLists.txt at src:

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.

Both libuv and xerces-c have a pkg-config ( .pc ) file, so you can reduce your CMakeLists.txt to the following. 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.

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 . Together, this will eliminate any errors about xerces and 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM