How to link Gurobi solver in a CMake project (c++, Clion)? - cmake

I am using Gurobi solver in a C++ project. However, when I tried to build the project in Clion, I got the error:
Undefined symbols for architecture x86_64:
and many lines like:
"__ZN6GRBEnv3setERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_", referenced from:
_main in main.cpp.o
and
__ZNSt3__16vectorIdNS_9allocatorIdEEE21__push_back_slow_pathIKdEEvRT_ in libgurobi_c++.a(LinExpr.o)
__ZNSt3__16vectorI6GRBVarNS_9allocatorIS1_EEE21__push_back_slow_pathIKS1_EEvRT_ in libgurobi_c++.a(LinExpr.o)
the error information also contains:
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[3]: *** [gurobitest] Error 1
make[2]: *** [CMakeFiles/gurobitest.dir/all] Error 2
make[1]: *** [CMakeFiles/gurobitest.dir/rule] Error 2
make: *** [gurobitest] Error 2
Here is my CmakeLists.txt:
cmake_minimum_required(VERSION 3.17)
project(gurobitest)
set(CMAKE_CXX_STANDARD 14)
set(GUROBI_INCLUDE_DIRECTORY "/library/gurobi912/mac64/include")
include_directories(${GUROBI_INCLUDE_DIRECTORY})
set(GUROBI_LIB_DIRECTORY "/library/gurobi912/mac64/lib")
link_directories(${GUROBI_LIB_DIRECTORY})
add_executable(gurobitest main.cpp ${GUROBI_INCLUDE_DIRECTORY}/gurobi_c++.h)
target_link_libraries(gurobitest gurobi_c++ gurobi91)
It seems I've already linked the library to the project properly, but it didn't work anyway. I really appreciate it for any help.

Related

x265 - Undefined symbols for architecture arm64 (Apple Silicon)

any ideas for this?
I try to build SDRangel on my M1 Mac but at 77% I get this error ...
-- GIT_EXECUTABLE /opt/local/bin/git
-- GIT LIVE REPO VERSION RETRIEVED
-- X265 RELEASE VERSION 3.5+68-40e37bce9
-- Detected CXX compiler using -O3 optimization level
-- Configuring done
-- Generating done
-- Build files have been written to: /opt/build/sdrangel/build/external/x265/src/x265-build
[ 27%] Performing build step for 'x265'
Consolidate compiler generated dependencies of target common
[ 45%] Built target common
Consolidate compiler generated dependencies of target encoder
[ 65%] Built target encoder
[ 76%] Built target x265-static
[ 77%] Linking CXX shared library libx265.dylib
Undefined symbols for architecture arm64:
"x265_entropyStateBits", referenced from:
_x265_costCoeffNxN_neon in pixel-util.S.o
(maybe you meant: _x265_entropyStateBits)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[5]: *** [libx265.206.dylib] Error 1
make[4]: *** [CMakeFiles/x265-shared.dir/all] Error 2
make[3]: *** [all] Error 2
make[2]: *** [external/x265/src/x265-stamp/x265-build] Error 2
make[1]: *** [external/CMakeFiles/x265.dir/all] Error 2
make: *** [all] Error 2
I am stuck. Don't know what to try to solve this issue.
I did not find anything anywhere ...
Working on it for most of 2 days, I did find this site that presents a working method of building libx265 stuff on Apple Silicon and works for me on macOS 13.0.1. https://www.osxexperts.net/ Problem is, it only builds the libraries for building it into ffmpeg, no executable binary.
When I tried to get the executable also, by removing -DENABLE_CLI=OFF from the last cmake command, the error comes back. Put it back in, everything completes without error.
Here is the full process that works for me (no executable). It builds a multilibrary that can encode 8, 10, or 12-bit video:
cd x265/source
#### 12 bit
cmake -DCMAKE_INSTALL_PREFIX:PATH=${TARGET} -DHIGH_BIT_DEPTH=ON -DMAIN12=ON -DENABLE_SHARED=NO -DEXPORT_C_API=NO -DENABLE_CLI=OFF .
make
mv libx265.a libx265_main12.a
make clean-generated
rm CMakeCache.txt
#### 10bit
cmake -DCMAKE_INSTALL_PREFIX:PATH=${TARGET} -DHIGH_BIT_DEPTH=ON -DMAIN10=ON -DENABLE_SHARED=NO -DEXPORT_C_API=NO -DENABLE_CLI=OFF .
make clean
make
mv libx265.a libx265_main10.a
make clean-generated && rm CMakeCache.txt
#### 8 bit and all together
# removed -DENABLE_CLI=OFF from last one so we get the x265 binary
cmake -DCMAKE_INSTALL_PREFIX:PATH=${TARGET} -DEXTRA_LIB="x265_main10.a;x265_main12.a" -DEXTRA_LINK_FLAGS=-L. -DLINKED_10BIT=ON -DLINKED_12BIT=ON -DENABLE_SHARED=OFF -DENABLE_CLI=OFF .
make clean
make
mv libx265.a libx265_main.a
# this is Apple's libtool
libtool -static -o libx265.a libx265_main.a libx265_main10.a libx265_main12.a
make install
When I remove the last -DENABLE_CLI=OFF, here's the error:
[100%] Linking CXX executable x265
Undefined symbols for architecture arm64:
"x265_entropyStateBits", referenced from:
_x265_costCoeffNxN_neon in libx265.a(pixel-util.S.o)
_x265_10bit_costCoeffNxN_neon in libx265_main10.a(pixel-util.S.o)
_x265_12bit_costCoeffNxN_neon in libx265_main12.a(pixel-util.S.o)
(maybe you meant: _x265_entropyStateBits)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [x265] Error 1
make[1]: *** [CMakeFiles/cli.dir/all] Error 2
make: *** [all] Error 2
JimsMBPro:source jim$
EDIT: #Peter Betz, see the issue report. Until the developers correct this error, it will compile if you edit x265/source/common/aarch64/pixel-util.S, line 2410. Add an underscore as follows. Change:
movrel x1, x265_entropyStateBits
to
movrel x1, _x265_entropyStateBits

_OBJC_CLASS_$_ShellTask error on objective-c project

I am getting this error:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_ShellTask", referenced from:
objc-class-ref in PhoneLocator.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Architectures of my target is $(ARCHS_STANDARD)
I have checked online throughout day :(. But I didn't get any solution. How can I resolve this issue.
_OBJC_CLASS_$_ShellTask this error means ShellTask this the file name. When you getting these sort of error it means that the file is missing under your compile sources. Just add that file the error will be gone
In target -> Build Phases -> Compile Sources I had all .m files including those that were giving issue.

Clion, cmake DSO missing when building simple2d

I have started with a project that are using the simple2d library (https://github.com/simple2d/simple2d). But when I try to build my small example project I get a DSO missing error. I think a have manage to link the dependencies for simple2d, but apparently I'm still some thing.
Here is the output:
> ====================[ Build | c_game | Debug ]==================================
/home/fredrik/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/192.5728.100/bin/cmake/linux/bin/cmake --build /home/fredrik/Repos/c_game/cmake-build-debug --target c_game -- -j 2 -m
[ 50%] Linking C executable c_game
/usr/bin/ld: /usr/local/lib/libsimple2d.a(gl.o): undefined reference to symbol 'fmin##GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/c_game.dir/build.make:85: recipe for target 'c_game' failed
make[3]: *** [c_game] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/c_game.dir/all' failed
make[2]: *** [CMakeFiles/c_game.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/c_game.dir/rule' failed
make[1]: *** [CMakeFiles/c_game.dir/rule] Error 2
Makefile:118: recipe for target 'c_game' failed
make: *** [c_game] Error 2
And here is my CMakeList.txt, I hope someone can help me.
cmake_minimum_required(VERSION 3.14)
project(c_game C)
set(CMAKE_C_STANDARD 11)
find_package(SDL2 REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(c_game main.c)
find_library(SIMPLE2D_LIB libsimple2d.a /usr/local/lib)
message(${SIMPLE2D_LIB})
message(${SDL2_INCLUDE_DIRS})
message(${GLEW_INCLUDE_DIRS})
target_include_directories(c_game PUBLIC ${SDL2_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS})
target_link_libraries(c_game ${SIMPLE2D_LIB} ${SDL2_LIBRARIES} ${GLEW_LIBRARIES})

How to link ICU4C in cmake via pkg-config on MacOS? [duplicate]

This question already has answers here:
Cmake cannot find library using "link_directories"
(4 answers)
Closed 3 years ago.
I work on macbook with newest OS. Installed icu4c library via homebrew. But I cannot link libicuuc correctly. In case of find library, I use
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/Cellar/icu4c/64.2/lib/pkgconfig
on my ~/.bashrc, but still got link error.
This is a very simple CMakeLists.txt:
cmake_minimum_required(VERSION 3.4)
project(xeditd CXX)
find_package(PkgConfig REQUIRED)
pkg_search_module(ICU_UC icu-uc)
set(LIB_DIR
${ICU_UC_LIBRARY_DIRS}
)
set(LIB
${ICU_UC_LIBRARIES}
)
set(INC_DIR
${ICU_UC_INCLUDE_DIRS}
)
set(SRC_FILE
xeditd.cpp
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -o2 ${ICU_UC_CXXFLAGS_OTHER}")
message(${LIB_DIR})
message(${LIB})
message(${INC_DIR})
message(${CMAKE_CXX_FLAGS})
add_executable(xeditd ${SRC_FILE})
link_directories(${LIB_DIR})
target_link_libraries(xeditd ${LIB})
target_include_directories(xeditd PUBLIC ${INC_DIR})
It generate:
/usr/local/Cellar/icu4c/64.2/lib (${LIB_DIR})
icuucicudata (${LIB})
/usr/local/Cellar/icu4c/64.2/include (${INC_DIR})
-std=c++14 -std=c++14 -o2 (${CMAKE_CXX_FLAGS})
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/rolin/workspace/github/xedit/.bin
[ 50%] Linking CXX executable xedit
ld: library not found for -licuuc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [xedit/xedit] Error 1
make[1]: *** [xedit/CMakeFiles/xedit.dir/all] Error 2
make: *** [all] Error 2
[ 50%] Linking CXX executable xeditd
ld: library not found for -licuuc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [xeditd/xeditd] Error 1
make[1]: *** [xeditd/CMakeFiles/xeditd.dir/all] Error 2
make: *** [all] Error 2
I don't known why libicuuc.dylib not found.
link_directories only affects targets that come AFTER it. So I move add_executable after the link_directories call.

cmake error with grpc in tensorflow

When trying to build tensorflow using cmake, I got the following error. I'm not sure whether it's an issue related to tensorflow or grpc. Any idea on what's going on?
This is on Mac OS Sierra.
steps to reproduce:
# clone tensorflow repo
# in tensorflow directory
cd tensorflow/contrib/cmake
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=/usr/local/bin/python3
make tf_tutorials_example_trainer
Errors:
# ...
Scanning dependencies of target tf_tutorials_example_trainer
[100%] Building CXX object CMakeFiles/tf_tutorials_example_trainer.dir/Users/kevenwang/VirtualBoxShared/another_tf/tensorflow/cc/tutorials/example_trainer.cc.o
[100%] Linking CXX executable tf_tutorials_example_trainer
Undefined symbols for architecture x86_64:
"_ares_cancel", referenced from:
on_readable_cb(grpc_exec_ctx*, void*, grpc_error*) in libgrpc_unsecure.a(grpc_ares_ev_driver_posix.cc.o)
on_writable_cb(grpc_exec_ctx*, void*, grpc_error*) in libgrpc_unsecure.a(grpc_ares_ev_driver_posix.cc.o)
"_ares_destroy", referenced from:
grpc_ares_ev_driver_unref(grpc_ares_ev_driver*) in libgrpc_unsecure.a(grpc_ares_ev_driver_posix.cc.o)
...
_grpc_ares_ev_driver_create in libgrpc_unsecure.a(grpc_ares_ev_driver_posix.cc.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[3]: *** [tf_tutorials_example_trainer] Error 1
make[2]: *** [CMakeFiles/tf_tutorials_example_trainer.dir/all] Error 2
make[1]: *** [CMakeFiles/tf_tutorials_example_trainer.dir/rule] Error 2
Maybe you install the grpc lib by Homebrew.
In that case , you need to link the c-ares lib.
e.g.
TARGET_LINK_LIBRARIES(demo "/usr/local/lib/libcares.a" ...)