【问题标题】:CMAKE MAC OSX library linking Issue: compiles on Linux but on on MacCMAKE MAC OSX 库链接问题:在 Linux 上编译但在 Mac 上
【发布时间】:2018-10-05 00:16:54
【问题描述】:

我正在尝试使用 CMake 编译我的代码。但是当我执行 make 命令时,出现以下错误:

[ 17%] Linking CXX shared library libglobal.so
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -dylib_install_name @rpath/libglobal.so -macosx_version_min 10.13.0 -o libglobal.so -L/opt/local/lib -L/Users/George/Documents/root/build/lib -lc++ -headerpad_max_install_names CMakeFiles/global.dir/RawHitPeta6.cpp.o CMakeFiles/global.dir/CorrectedHit.cpp.o CMakeFiles/global.dir/CalibrationSettings.cpp.o CMakeFiles/global.dir/Coincidence.cpp.o CMakeFiles/global.dir/AnalysisSettings.cpp.o CMakeFiles/global.dir/Plot.cpp.o CMakeFiles/global.dir/Destination.cpp.o CMakeFiles/global.dir/Crystal.cpp.o CMakeFiles/global.dir/Geometry.cpp.o CMakeFiles/global.dir/geometries/TwoModuleSetup.cpp.o CMakeFiles/global.dir/geometries/P6EvaluationBoard.cpp.o CMakeFiles/global.dir/geometries/SystemSingleLayer7.cpp.o CMakeFiles/global.dir/geometries/SystemSingleLayer8.cpp.o CMakeFiles/global.dir/geometries/SystemDualLayer.cpp.o CMakeFiles/global.dir/geometries/SystemHighResolutionDualLayer.cpp.o CMakeFiles/global.dir/geometries/SystemHighResolutionDualLayerLong.cpp.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.1.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "TVersionCheck::TVersionCheck(int)", referenced from:
      __GLOBAL__sub_I_Plot.cpp in Plot.cpp.o
      __GLOBAL__sub_I_Destination.cpp in Destination.cpp.o
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]: *** [Analysis/global/libglobal.so] Error 1
make[1]: *** [Analysis/global/CMakeFiles/global.dir/all] Error 2
make: *** [all] Error 2

我已经在寻找 ld: symbol(s) not found for architecture x86_64 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)。

This is the cmake outout I get: 

CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default.  Run "cmake
  --help-policy CMP0042" for policy details.  Use the cmake_policy command to
  set the policy and suppress this warning.

  MACOSX_RPATH is not specified for the following targets:

   coincidencePlots
   correctedHitPlots
   generalPlots
   global
   processing
   rawHitPlots

This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done
-- Build files have been written to: /Users/George/Desktop/petaAnalysis/build

我已经在寻找 ld: symbol(s) not found for architecture x86_64 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)。但我找不到任何有用的东西,这有帮助。

这是我得到的 cmake 输出:

CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default.  Run "cmake
--help-policy CMP0042" for policy details.  Use the cmake_policy command to
set the policy and suppress this warning.

MACOSX_RPATH is not specified for the following targets:

coincidencePlots
correctedHitPlots
generalPlots
global
processing
countRawPlots

This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done
-- Build files have been written to: /Users/Hans/Desktop/Project/build

我试图在 linux 上运行代码。那里似乎工作正常。我认为这是我创建的共享库之间的链接问题。

这是 CMAKE 文件: 文件1(在主目录中):

cmake_minimum_required(VERSION 2.8)

project(Analysis) 
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${Analysis_SOURCE_DIR}/bin)


SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -stdlib=libc++ -lstdc++ -std=c++14 -m64 -I/Users/George/Documents/root/build/include")


#SET(CMAKE_BUILD_WITH_INSTALL_RPATH true)
#SET(CMAKE_INSTALL_RPATH "$ORIGIN/")
SET(CMAKE_SYSTEM_NAME Darwin)

# Add MacPorts
INCLUDE_DIRECTORIES(/opt/local/include)
LINK_DIRECTORIES(/opt/local/lib)

ADD_SUBDIRECTORY(Analysis)
ADD_SUBDIRECTORY(CrystalMapGenerator)
ADD_SUBDIRECTORY(CoincidenceBrowser)
ADD_SUBDIRECTORY(FunctionTests)
ADD_SUBDIRECTORY(AnalysisResultExtractor)
ADD_SUBDIRECTORY(AnalysisComparison)
ADD_SUBDIRECTORY(SDIPRawDataDebug)
ADD_SUBDIRECTORY(GateDataTimeCalibrationTester)

文件2(在子目录中):

cmake_minimum_required(VERSION 2.8)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${Analysis_SOURCE_DIR}/bin)

if(UNIX)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x")
endif()

#set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_BUILD_TYPE Release)

# Add information on ROOT libraries
INCLUDE(../FindROOT.cmake)
INCLUDE_DIRECTORIES( ${ROOTSYS}/include)
LINK_DIRECTORIES( ${ROOTSYS}/lib)

ADD_SUBDIRECTORY(processing)
ADD_SUBDIRECTORY(generalPlots)
ADD_SUBDIRECTORY(rawHitPlots)
ADD_SUBDIRECTORY(correctedHitPlots)
ADD_SUBDIRECTORY(coincidencePlots)
ADD_SUBDIRECTORY(global)

include_directories("processing")
include_directories("generalPlots")
include_directories("rawHitPlots")
include_directories("correctedHitPlots")
include_directories("coincidencePlots")
include_directories("global")

set(SOURCES
    DataChain.cpp
    main.cpp
)

ADD_EXECUTABLE(analysis ${SOURCES})
target_link_libraries(analysis processing generalPlots rawHitPlots correctedHitPlots coincidencePlots global ${ROOT_LIBRARIES} -lSpectrum -lMinuit)

File3(在全局库文件夹中):

cmake_minimum_required(VERSION 2.8)

INCLUDE(../../FindROOT.cmake)
INCLUDE_DIRECTORIES( ${ROOT_INCLUDE_DIR})
LINK_DIRECTORIES( ${ROOT_LIBRARY_DIR})

ADD_LIBRARY(global SHARED
    RawHitPeta6.cpp
    CorrectedHit.cpp    
    CalibrationSettings.cpp
    Coincidence.cpp
    AnalysisSettings.cpp
    Plot.cpp
    Destination.cpp

    Crystal.cpp
    Geometry.cpp
    geometries/TwoModuleSetup.cpp
    geometries/P6EvaluationBoard.cpp
    geometries/SystemSingleLayer7.cpp
    geometries/SystemSingleLayer8.cpp
    geometries/SystemDualLayer.cpp
    geometries/SystemHighResolutionDualLayer.cpp
    geometries/SystemHighResolutionDualLayerLong.cpp
)

非常感谢!

【问题讨论】:

  • 我敢打赌,您没有从 ROOT 链接到您需要的正确库。您可以使用make VERBOSE=1 查看完整的编译/链接命令。

标签: c++ macos cmake linker-errors root-framework


【解决方案1】:

如果您使用的是最新版本的 ROOT5 或 ROOT6,而不是编写或下载 FindROOT.cmake,请使用 ROOT 在您(或您的软件包系统)构建它时为您自动生成的信息。

在您的 ROOT 安装中,找到包含 ROOTConfig.cmake 的目录。将该目录添加到您的CMAKE_MODULE_PATH。然后,拨打find_package(ROOT)

基本的CMakeLists.txt 可能如下所示:

cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)

execute_process(COMMAND root-config --prefix 
                COMMAND tr -d \\n
                RESULT_VARIABLE RC_RESULT 
                OUTPUT_VARIABLE ROOT_PREFIX)

list(APPEND CMAKE_MODULE_PATH "${ROOT_PREFIX}/share/root/cmake")
find_package(ROOT)

include(CMakePrintHelpers)
cmake_print_variables(ROOT_FOUND ROOT_LIBRARIES ROOT_INCLUDE_DIRS)

运行 cmake 会导致:

~/rt
❯ cmake .                                                   
-- The C compiler identification is AppleClang 9.1.0.9020039
-- The CXX compiler identification is AppleClang 9.1.0.9020039
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- ROOT_FOUND="1" ; ROOT_LIBRARIES="/usr/local/lib/root/libCore.so;/usr/local/lib/root/libImt.so;/usr/local/lib/root/libRIO.so;/usr/local/lib/root/libNet.so;/usr/local/lib/root/libHist.so;/usr/local/lib/root/libGraf.so;/usr/local/lib/root/libGraf3d.so;/usr/local/lib/root/libGpad.so;/usr/local/lib/root/libTree.so;/usr/local/lib/root/libTreePlayer.so;/usr/local/lib/root/libRint.so;/usr/local/lib/root/libPostscript.so;/usr/local/lib/root/libMatrix.so;/usr/local/lib/root/libPhysics.so;/usr/local/lib/root/libMathCore.so;/usr/local/lib/root/libThread.so;/usr/local/lib/root/libMultiProc.so" ; ROOT_INCLUDE_DIRS="/usr/local/include/root"
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/nega/rt

~/rt
❯ 

您会注意到ROOT_FOUND 已设置为TRUE,并且ROOT_LIBRARIESROOT_INCLUDE_DIRS 已被填充。您现在可以使用这些变量来设置编译和链接。

【讨论】:

  • 为了完整起见,root 项目还提供了说明如何在 cmake 项目中使用 root root.cern.ch/how/integrate-root-my-project-cmake
  • 嗨,nega 和 pseyfert。非常感谢你帮助我。我试图包括您的更改。但我仍然得到同样的错误。我包括了我从 commangs nega 建议的输出并将它们放入: SET(SET(ROOT_LIBRARIES="/Applications/root_v6.12.06/lib/libCore.so;....) 和 SET(ROOT_INCLUDE_DIRS="/Applications/ root_v6.12.06/include")。但我仍然有同样的错误。我链接错了吗?
  • 直接使用变量。不要从中提取值并设置新变量。成功运行find_package(ROOT) 后,您将获得ROOT_FOUND ROOT_LIBRARIESROOT_INCLUDE_DIRS。在对include_directories()target_include_directories()add_library()target_link_libraries() 的调用中使用这些变量。
  • 我将此行添加到 cmake。 INCLUDE_DIRECTORIES(${ROOTSYS_INCLUDE_DIRS}) 和 target_link_libraries(analysis ${ROOT_LIBRARIES}) 仍然有问题。不知道是不是root的问题。因为我写了另一个文件来测试根库,它似乎编译得很好。
  • 我想我找到了问题的核心。我尝试了几个构建。我唯一遇到这个问题的时候,是当我将一些根函数添加到共享库时。 libcontents.cpp 中的 add_libray(libname SHARED libcontents.cpp) 是根函数,无法编译。但是,如果我将它作为 main.cpp 中的示例取出并取出根函数,那么它编译得很好。如何“显示”共享库(位于子目录中),根库在哪里?
猜你喜欢
  • 2014-06-20
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
  • 2010-10-08
  • 2011-06-26
相关资源
最近更新 更多