在Mac上编译动态库:ld:找不到架构x86_64的符号

时间:2015-03-16 17:03:33

标签: c++ macos linker cmake scip

我正致力于编译SCIP动态库。 SCIP是用C实现的,我们的优化项目都是Java,因此我们希望使用JNA来访问它。我们编写了一个C ++包装器来构建动态库,我在Mac上编译时遇到了问题(运行Yosemite 10.10.2)。这是我的gcc和g ++版本:

> gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
> g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

我们正在使用CMake(版本3.2.1)来构建项目。这是我们的主要CMakeLists.txt

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -O2 -Wall -DNDEBUG")

if(CMAKE_COMPILER_IS_GNUCXX)
    if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fPIC"  )
        set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fPIC"  )    
    endif()
endif()

if(APPLE)
    set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x")
    set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
    set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
    set(CMAKE_MACOSX_RPATH 1)
    set(CMAKE_OSX_ARCHITECTURES "x86_64")
endif()

if(WIN32)
    add_definitions(-DUNICODE -D_UNICODE)
    set(TARGET_ARCHITECTURE "x86")
endif()

set(LIB_TYPE SHARED)
include_directories("/path/to/scipoptsuite-3.1.1/scip-3.1.1/src")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
add_library(scipLibrary ${LIB_TYPE} "stdafx.cpp")

这是make VERBOSE=1的输出:

/usr/local/Cellar/cmake/3.2.1/bin/cmake -H/path/to/scip -B/path/to/scip/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.2.1/bin/cmake -E cmake_progress_start /path/to/scip/build/CMakeFiles /path/to/scip/build/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f source/CMakeFiles/scipLibrary.dir/build.make source/CMakeFiles/scipLibrary.dir/depend
cd /path/to/scip/build && /usr/local/Cellar/cmake/3.2.1/bin/cmake -E cmake_depends "Unix Makefiles" /path/to/scip /path/to/scip/source /path/to/scip/build /path/to/scip/build/source /path/to/scip/build/source/CMakeFiles/scipLibrary.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f source/CMakeFiles/scipLibrary.dir/build.make source/CMakeFiles/scipLibrary.dir/build
/usr/local/Cellar/cmake/3.2.1/bin/cmake -E cmake_progress_report /path/to/scip/build/CMakeFiles 1
[100%] Building CXX object source/CMakeFiles/scipLibrary.dir/stdafx.cpp.o
cd /path/to/scip/build/source && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++   -DscipLibrary_EXPORTS -std=c++0x -O2 -Wall -DNDEBUG -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -fPIC -I/path/to/scipoptsuite-3.1.1/scip-3.1.1/src -I/path/to/scip/source    -o CMakeFiles/scipLibrary.dir/stdafx.cpp.o -c /path/to/scip/source/stdafx.cpp
Linking CXX shared library ../output/libscipLibrary.dylib
cd /path/to/scip/build/source && /usr/local/Cellar/cmake/3.2.1/bin/cmake -E cmake_link_script CMakeFiles/scipLibrary.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++   -std=c++0x -O2 -Wall -DNDEBUG -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -dynamiclib -Wl,-headerpad_max_install_names  -o ../output/libscipLibrary.dylib -install_name @rpath/libscipLibrary.dylib CMakeFiles/scipLibrary.dir/stdafx.cpp.o 
Undefined symbols for architecture x86_64:
  "_SCIPaddBilinTermQuadratic", referenced from:
      _LibSCIPaddBilinTermQuadratic in stdafx.cpp.o
  "_SCIPaddCoefLinear", referenced from:
      _LibSCIPaddCoefLinear in stdafx.cpp.o
  "_SCIPaddCons", referenced from:
      _LibSCIPaddCons in stdafx.cpp.o
  "_SCIPaddQuadVarLinearCoefQuadratic", referenced from:
      _LibSCIPaddQuadVarLinearCoefQuadratic in stdafx.cpp.o
  "_SCIPaddSquareCoefQuadratic", referenced from:
      _LibSCIPaddSquareCoefQuadratic in stdafx.cpp.o
  "_SCIPaddVar", referenced from:
      _LibSCIPaddVar in stdafx.cpp.o
  "_SCIPblkmem", referenced from:
      _LibSCIPexprCreateLinear in stdafx.cpp.o
      _LibSCIPexprCreate in stdafx.cpp.o
      _LibSCIPexprCreateMonomial in stdafx.cpp.o
      _LibSCIPexprCreatePolynomial in stdafx.cpp.o
      _LibSCIPexprtreeCreate in stdafx.cpp.o
  "_SCIPchgVarLb", referenced from:
      _LibSCIPchgVarLb in stdafx.cpp.o
  "_SCIPchgVarObj", referenced from:
      _LibSCIPchgVarObj in stdafx.cpp.o
  "_SCIPchgVarUb", referenced from:
      _LibSCIPchgVarUb in stdafx.cpp.o
  "_SCIPcreate", referenced from:
      _LibSCIPcreate in stdafx.cpp.o
  "_SCIPcreateConsBasicAbspower", referenced from:
      _LibSCIPcreateConsBasicAbspower in stdafx.cpp.o
  "_SCIPcreateConsBasicLinear", referenced from:
      _LibSCIPcreateConsBasicLinear in stdafx.cpp.o
  "_SCIPcreateConsBasicNonlinear", referenced from:
      _LibSCIPcreateConsBasicNonlinear in stdafx.cpp.o
  "_SCIPcreateConsBasicQuadratic", referenced from:
      _LibSCIPcreateConsBasicQuadratic in stdafx.cpp.o
  "_SCIPcreateConsBasicSetpart", referenced from:
      _LibSCIPcreateConsBasicSetpart in stdafx.cpp.o
  "_SCIPcreateProbBasic", referenced from:
      _LibSCIPcreateProbBasic in stdafx.cpp.o
  "_SCIPcreateVarBasic", referenced from:
      _LibSCIPcreateVarBasic in stdafx.cpp.o
  "_SCIPexprCreate", referenced from:
      _LibSCIPexprCreate in stdafx.cpp.o
  "_SCIPexprCreateLinear", referenced from:
      _LibSCIPexprCreateLinear in stdafx.cpp.o
  "_SCIPexprCreateMonomial", referenced from:
      _LibSCIPexprCreateMonomial in stdafx.cpp.o
  "_SCIPexprCreatePolynomial", referenced from:
      _LibSCIPexprCreatePolynomial in stdafx.cpp.o
  "_SCIPexprtreeCreate", referenced from:
      _LibSCIPexprtreeCreate in stdafx.cpp.o
  "_SCIPexprtreeFree", referenced from:
      _LibSCIPexprtreeFree in stdafx.cpp.o
  "_SCIPexprtreeSetVars", referenced from:
      _LibSCIPexprtreeSetVars in stdafx.cpp.o
  "_SCIPfree", referenced from:
      _LibSCIPfree in stdafx.cpp.o
  "_SCIPfreeTransform", referenced from:
      _LibSCIPfreeTransform in stdafx.cpp.o
  "_SCIPgetBestSol", referenced from:
      _LibSCIPprintSolution in stdafx.cpp.o
      _LibSCIPgetBestSol in stdafx.cpp.o
  "_SCIPgetNSols", referenced from:
      _LibSCIPgetNSols in stdafx.cpp.o
  "_SCIPgetStatus", referenced from:
      _LibSCIPgetStatus in stdafx.cpp.o
  "_SCIPincludeDefaultPlugins", referenced from:
      _LibSCIPincludeDefaultPlugins in stdafx.cpp.o
  "_SCIPmajorVersion", referenced from:
      _LibSCIPmajorVersion in stdafx.cpp.o
  "_SCIPmessagePrintError", referenced from:
      _LibSCIPincludeDefaultPlugins in stdafx.cpp.o
      _LibSCIPcreateProbBasic in stdafx.cpp.o
      _LibSCIPchgVarLb in stdafx.cpp.o
      _LibSCIPchgVarUb in stdafx.cpp.o
      _LibSCIPaddVar in stdafx.cpp.o
      _LibSCIPexprtreeSetVars in stdafx.cpp.o
      _LibSCIPexprtreeFree in stdafx.cpp.o
      ...
  "_SCIPmessagePrintErrorHeader", referenced from:
      _LibSCIPincludeDefaultPlugins in stdafx.cpp.o
      _LibSCIPcreateProbBasic in stdafx.cpp.o
      _LibSCIPchgVarLb in stdafx.cpp.o
      _LibSCIPchgVarUb in stdafx.cpp.o
      _LibSCIPaddVar in stdafx.cpp.o
      _LibSCIPexprtreeSetVars in stdafx.cpp.o
      _LibSCIPexprtreeFree in stdafx.cpp.o
      ...
  "_SCIPminorVersion", referenced from:
      _LibSCIPminorVersion in stdafx.cpp.o
  "_SCIPpresolve", referenced from:
      _LibSCIPpresolve in stdafx.cpp.o
  "_SCIPprintOrigProblem", referenced from:
      _LibSCIPprintProblemCIP in stdafx.cpp.o
      _LibSCIPprintProblemMPS in stdafx.cpp.o
      _LibSCIPprintProblemLP in stdafx.cpp.o
      _LibSCIPprintProblemCIPToFile in stdafx.cpp.o
      _LibSCIPprintProblemMPSToFile in stdafx.cpp.o
      _LibSCIPprintProblemLPToFile in stdafx.cpp.o
  "_SCIPprintSol", referenced from:
      _LibSCIPprintSolution in stdafx.cpp.o
  "_SCIPreleaseCons", referenced from:
      _LibSCIPreleaseCons in stdafx.cpp.o
  "_SCIPreleaseVar", referenced from:
      _LibSCIPreleaseVar in stdafx.cpp.o
  "_SCIPsetObjsense", referenced from:
      _LibSCIPsetObjsense in stdafx.cpp.o
  "_SCIPsolve", referenced from:
      _LibSCIPsolve in stdafx.cpp.o
  "_SCIPsubversion", referenced from:
      _LibSCIPsubversion in stdafx.cpp.o
  "_SCIPtechVersion", referenced from:
      _LibSCIPtechVersion in stdafx.cpp.o
  "_SCIPvarGetLPSol_rec", referenced from:
      solGetArrayVal(SCIP_Sol*, SCIP_Var*) in stdafx.cpp.o
  "_SCIPvarGetNLPSol_rec", referenced from:
      solGetArrayVal(SCIP_Sol*, SCIP_Var*) in stdafx.cpp.o
  "_SCIPvarGetOrigvarSum", referenced from:
      _LibSCIPsolGetVal in stdafx.cpp.o
  "_SCIPvarGetPseudoSol", referenced from:
      solGetArrayVal(SCIP_Sol*, SCIP_Var*) in stdafx.cpp.o
  "_SCIPversion", referenced from:
      _LibSCIPversion in stdafx.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]: *** [output/libscipLibrary.dylib] Error 1
make[1]: *** [source/CMakeFiles/scipLibrary.dir/all] Error 2
make: *** [all] Error 2

您可以看到问题是链接器给我们ld: symbol(s) not found for architecture x86_64错误。我经常搜索以找出如何解决这个问题,但我没有运气。

我不确定是否有必要提供stdafx.hstdafx.cpp的内容,所以我暂时不让它们出来,但如果它&#39必要的,让我知道,我可以提供。

1 个答案:

答案 0 :(得分:1)

好吧,我犯了一个错误,我没有构建scipoptlib,因此没有可以构建的库(抱歉,我以前从未创建过动态库)。只需在make scipoptlib IPOPT=true SHARED=true中运行/path/to/scipoptsuite-3.1.1即可。

以下是引用单个共享CMakeLists.txt的更新scipoptlib

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -O2 -Wall -DNDEBUG")

if(CMAKE_COMPILER_IS_GNUCXX)
    if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
        set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
    endif()
endif()

if(APPLE)
    set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x")
    set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
    set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
    set(CMAKE_MACOSX_RPATH 1)
endif()

if(WIN32)
    add_definitions(-DUNICODE -D_UNICODE)
    set(TARGET_ARCHITECTURE "x86")
endif()

include_directories("/path/to/scipoptsuite-3.1.1/scip-3.1.1/src/")
add_library(scipLibrary SHARED "stdafx.cpp")
target_link_libraries(scipLibrary "/path/to/scipoptsuite-3.1.1/lib/libscipopt-3.1.1.darwin.x86_64.gnu.opt.so")

我现在可以实际生成dylib,这太棒了!然而,现在JNA正在呕吐(说它无法找到dylib),但这可能是另一个SO线程的话题。