CMAKE MAC OSX库链接问题:在Linux上编译但在Mac上编译

时间:2018-04-24 16:30:20

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

我正在尝试使用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

我已经找到了找不到架构x86_64的ld:符号     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

我已经找到了找不到架构x86_64的ld:符号 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
)

非常感谢!

2 个答案:

答案 0 :(得分:0)

如果您使用的是最新版本的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。您现在可以使用这些变量来设置编译和链接。

答案 1 :(得分:0)

VERBOSE = 1给出了以下错误:

[  1%] Linking CXX shared library libglobal.dylib
    cd /Users/Francis/Desktop/petaAnalysis/build/Analysis/global && /opt/local/bin/cmake -E cmake_link_script CMakeFiles/global.dir/link.txt --verbose=1
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -Wall -std=gnu++0x -O3 -DNDEBUG -dynamiclib -Wl,-headerpad_max_install_names  -o libglobal.dylib -install_name /Users/Francis/Desktop/petaAnalysis/build/Analysis/global/libglobal.dylib 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  -L/Applications/root_v6.12.06/lib 
    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.dylib] Error 1
    make[1]: *** [Analysis/global/CMakeFiles/global.dir/all] Error 2
    make: *** [all] Error 2