macOS High Sierra下CMake链接Bonmin报错:ld: framework not found -lAccelerate
Hey there, let’s work through this Bonmin linking problem together. Since the native Makefile for the Bonmin example compiles fine, the issue almost certainly stems from how CMake is locating or binding Bonmin’s libraries and its COIN-OR dependencies. Here are targeted fixes to get your project running:
1. Validate Your Bonmin Build Configuration
First, double-check the flags you used when compiling Bonmin — mismatched build settings are a common culprit:
- When you ran
../configure -C --d..., make sure you added a C++ standard flag that matches your CMake project (Xcode 9.3 defaults to C++14). For example:../configure -C --enable-shared --with-cxxflags="-std=c++14" - Confirm you built Bonmin as a shared library with
--enable-shared— static libraries can cause linking headaches on macOS, especially with COIN-OR’s nested dependencies.
2. Explicitly Tell CMake Where to Find Bonmin
CMake needs clear paths to Bonmin’s headers and libraries. Add these lines to your CMakeLists.txt:
# Replace with your actual Bonmin installation path set(BONMIN_INSTALL_DIR "/PATH_TO_BONMIN/Bonmin") # Include Bonmin header directories include_directories( ${BONMIN_INSTALL_DIR}/include ${BONMIN_INSTALL_DIR}/../CoinUtils/include ${BONMIN_INSTALL_DIR}/../Osi/include ${BONMIN_INSTALL_DIR}/../Cbc/include ) # Locate and link Bonmin + its dependencies find_library(BONMIN_LIB bonmin PATHS ${BONMIN_INSTALL_DIR}/lib) find_library(COINUTILS_LIB CoinUtils PATHS ${BONMIN_INSTALL_DIR}/../CoinUtils/lib) find_library(OSI_LIB Osi PATHS ${BONMIN_INSTALL_DIR}/../Osi/lib) find_library(CBC_LIB Cbc PATHS ${BONMIN_INSTALL_DIR}/../Cbc/lib) target_link_libraries(YourProjectTargetName ${BONMIN_LIB} ${COINUTILS_LIB} ${OSI_LIB} ${CBC_LIB} # Add other COIN-OR libraries as needed (e.g., Cgl) )
Don’t forget to replace /PATH_TO_BONMIN with your actual installation path and YourProjectTargetName with your CMake target’s name.
3. Fix Xcode-Specific Linking Quirks
macOS and Xcode have unique behavior when it comes to library loading:
- Add
set(CMAKE_MACOSX_RPATH ON)to yourCMakeLists.txt— this ensures the runtime can find Bonmin’s libraries when you run your executable, avoiding "library not found" errors. - If you’re using static Bonmin libraries, add this flag to force linking all symbols (COIN-OR static libs often require this on macOS):
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-all_load")
4. Compare Working Makefile vs. CMake Build Logs
Since the Bonmin example’s Makefile works, use it as a reference:
- Run
make VERBOSE=1in the/PATH_TO_BONMIN/Bonmin/examples/CppExampledirectory to see the exact compiler/linker flags the working build uses. - In your CMake project, run
cmake --build . --verboseto get its build commands. Look for differences in:- Include paths (
-Iflags) - Library paths (
-Lflags) - Linked libraries (
-lflags) - C++ standard flags (
-std=...)
Copy any missing flags from the working Makefile into yourCMakeLists.txtusingadd_compile_options()ortarget_compile_options().
- Include paths (
5. Ensure Toolchain Consistency
Xcode 9.3 uses Apple LLVM 9.1.0 — make sure Bonmin was compiled with the same compiler:
- Re-run
configurewith explicit compiler paths to match Xcode’s clang:../configure -C --enable-shared --with-cxxflags="-std=c++14" CC=/usr/bin/clang CXX=/usr/bin/clang++
This avoids ABI mismatches between Bonmin and your CMake project.
If you still hit errors, share the exact linking output from CMake — that’ll help narrow down the issue even more!
内容的提问来源于stack exchange,提问作者Franzi

