I want to use casadi with c++ for optimal control problems. Therefore, I want to include it using the binary with cmake. I am on a Ubuntu 20.04 machine. My cmake version is 3.16.3. My gcc version is 11.1.0.
I followed the following example: Integrate pre-compiled libraries into C++ codebase with CMake ExternalProject and also added this flag
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
for compiling into my CMakeLists.txt https://groups.google.com/g/casadi-users/c/xIw4U6Rymps?pli=1
My code structures as follows (as in the exmaple):
- main.cpp
- CMakeLists.txt
- external/CMakeLists.txt
- build/
I build the exe as follows in the build folder:
cmake ..
make
and then executing the generated exe:
./main
It compiles without any problem but when executing i receive the following error message:
./main: symbol lookup error: ./main: undefined symbol: _ZN6casadi6MatrixINS_6SXElemEE4_symERKSsRKNS_8SparsityE
It only complains, when adding the SX Casadi symbols. Without them everything works! I think it is some sort of linking error. Thanks for your help. I appreciate every help :) . I post below my code.
main.cpp:
#include <casadi/casadi.hpp>
using namespace casadi;
using namespace std;
int main()
{
casadi_printf("This works! \n");
SX x = SX::sym("x");
SX z = SX::sym("z");
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(untitled)
set(CMAKE_CXX_STANDARD 17)
# some default target
add_executable(main main.cpp)
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
# Configure and build external project
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/external)
execute_process(
COMMAND ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR}/external
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/external
)
execute_process(
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/external
)
# find and link externals
find_package(casadi REQUIRED HINTS ${CMAKE_BINARY_DIR}/external/external/casadi/src/casadi-3.5.5/casadi)
target_link_libraries(main casadi)
external/CMakesLists.txt
cmake_minimum_required(VERSION 3.10)
project(external)
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
include(ExternalProject)
ExternalProject_Add(
casadi-3.5.5
URL https://github.com/casadi/casadi/releases/download/3.5.5/casadi-linux-py39-v3.5.5-64bit.tar.gz
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
PREFIX ${CMAKE_BINARY_DIR}/external/casadi)
I tried: including an older binary of casadi, removing and adding the flag
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
in both CMakeLists.txt