Search code examples
c++macoslinkercmakescip

compiling dynamic library on Mac: ld: symbol(s) not found for architecture x86_64


I'm working on compiling a SCIP dynamic library. SCIP is implemented in C, and our optimization project is all Java, so we're wanting to use JNA to access it. We've written a C++ wrapper to build the dynamic library, and I'm having issues compiling it on my Mac (running Yosemite 10.10.2). Here's my version of gcc and 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

We're using CMake (version 3.2.1) to build the project. Here's our main 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")

Here's the output of 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

You can see that the problem is that the linker is giving us an ld: symbol(s) not found for architecture x86_64 error. I've searched a lot to try and figure out how to solve this, but I haven't had any luck.

I'm not sure it's necessary to provide the contents of stdafx.h or stdafx.cpp, so I'll leave them out for now, but if it's necessary, let me know, and I can provide them.


Solution

  • Ok, I had made a mistake where I didn't build scipoptlib, so there was no library to build against (sorry, I've never created a dynamic library before). Running make scipoptlib IPOPT=true SHARED=true in /path/to/scipoptsuite-3.1.1 was all that was needed to do that.

    Here's the updated CMakeLists.txt that references the single shared 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")
    

    I can actually generate the dylib now, which is great! However, now JNA is throwing up (saying it can't find the dylib), but that's probably the topic of another SO thread.