Cannot run simple example in Casadi for C++

1,093 views
Skip to first unread message

fdev

unread,
Mar 22, 2022, 12:56:36 PM3/22/22
to CasADi
I am trying to integrate Casadi into a C++ codebase using the CMake ExternalProject functionality. To this end, I followed the minimal example given here. It works, but unfortunately not completely! Indeed, if I replace the contents of the main.cpp file with the ones of another Casadi example, then I get a series of errors:
[build] /home/<path>/main.cpp:56: undefined reference to `casadi::nlpsol(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, casadi::MX, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, casadi::MX> > > const&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, casadi::GenericType, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, casadi::GenericType> > > const&)'
[build] /usr/bin/ld: /home/<path>/main.cpp:59: undefined reference to `casadi::Function::generate_dependencies(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, casadi::GenericType, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, casadi::GenericType> > > const&) const'
[build] /usr/bin/ld: /home/<path>/main.cpp:65: undefined reference to `casadi::nlpsol(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, casadi::GenericType, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, casadi::GenericType> > > const&)'
[build] /usr/bin/ld: /home/<path>/main.cpp:72: undefined reference to `casadi::nlpsol(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, casadi::GenericType, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, casadi::GenericType> > > const&)'
[build] /usr/bin/ld: /home/<path>/main.cpp:84: undefined reference to `casadi::Function::operator()(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, casadi::Matrix<double>, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, casadi::Matrix<double> > > > const&) const'

Does anyone know how to fix this issue? I thought that by finding the Casadi package I
would have automatically got IPOPT. What am I missing?

Justin B

unread,
Jun 27, 2022, 9:01:04 PM6/27/22
to CasADi
Hello,

Sorry this is late, but I ran into the same problem when I was writing my C++ project with CasADi. I don't think it has anything to do with IPOPT, as the provided binary distributions include the IPOPT interface. This article explains the error:

If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly happens when linking to a third-party library that was compiled with an older version of GCC. If the third-party library cannot be rebuilt with the new ABI then you will need to recompile your code with the old ABI.

I was able to fix it by adding this line to my CMakeLists.txt: 

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

This worked on my Ubuntu WSL.

I hope you've found a fix by this point, but I hope this helps anyone else having the issue!

Justin B

Tong Zhao

unread,
Jan 22, 2023, 8:03:21 PM1/22/23
to CasADi
Hello Justin and fdev,

I have a similar problem in linking the CasADi 3.5.5 MATLAB API binary package with my minimal C++-based casadi problem code. I am using Visual Studio as the IDE and in my main.cpp file, I have the following:

#include <iostream>
#include <casadi/casadi.hpp>

using namespace casadi;
using namespace std;

int main() {

    // Declare variable
    SX x = SX::sym("x");
    cout << x << endl;
    return 0;
}

To make my Visual Studio C++ project recognize the CasADi library, I added the CasADi package's include folder (C:\Program Files\casadi-windows-matlabR2016a-v3.5.5\include) as 'Additional include directories' in my Visual Studio project property panel. This addition allowed the IDE to recognize the CasADi namespace and type definitions. But I am not sure if this addition is good enough to also make sure that my c++ program is linking to the CasADi library. Anyway, if I try to build my C++ project containing this main.cpp file in Visual Studio, I will get dozens of linking errors (LNK2019 for the VS error code), and the first one is the following:

Severity    Code    Description    Project    File    Line    Suppression State
Error    LNK2019    unresolved external symbol "__declspec(dllimport) public: __cdecl casadi::SXElem::~SXElem(void)" (__imp_??1SXElem@casadi@@QEAA@XZ) referenced in function "public: void * __cdecl casadi::SXElem::`scalar deleting destructor'(unsigned int)" (??_GSXElem@casadi@@QEAAPEAXI@Z)    MainApp    C:\github\vehicle-MPC-CasADi-CPP\src\MainApp\MainApp\MainApp.obj    1    

Do you have any idea why these linking errors come out? Thanks.

Horea Alexandru Caramizaru

unread,
Jan 23, 2023, 11:33:48 AM1/23/23
to CasADi
Hi,
Are you using the right paths for the lib and headers? To check it, you can start CasADi in Python and query the location using
GlobalOptions.getCasadiIncludePath()
GlobalOptions.getCasadiPath()
for your particular CasADi installation.

Best,
Horea C.

Tong Zhao

unread,
Jan 23, 2023, 9:16:16 PM1/23/23
to CasADi
Hi Horea,

Thank you for your suggestion. However, after downloading the CasADi python binary and finding our the include path, and adding the include path to the Additional Include Directories in my path, it does not change the error message. I got the same linking errors.

I believe this problem may not have an easy solution, since Joris mentioned in this other thread that

QUOTE FROM JORIS
We do not currently ship Visual-Studio compatible libraries.
All our official binaries are prepared with MinGW.

Your best bet is to build CasADi yourself using Visual Studio.
END QUOTE

I will try something else: I will try to stay away from Visual Studio, and use CMake to link and build my simplest C++ casadi program. I will post my results later.

Horea Alexandru Caramizaru

unread,
Jan 24, 2023, 11:42:41 AM1/24/23
to CasADi
Hi,

To check your theory, you can try to use wsl (under win10/11). Using CMake under Windows will not change much if you don't already have MinGW. The current visual studio version is using CMake anyway.
You can also inspect the CasADi dll/lib to check out your assumption (whether the current official binaries are prepared with MinGW or not)
In my previous tryings, I observed that CasADi's version from the official download page is different than the one you get by using python pip install/conda...

Btw, from your previous message, I get the impression that you only set the path for headers and not for binaries as well... if that is the case, this might be one of the reasons for your problems.

Best,
Horea C.

Tong Zhao

unread,
Jan 28, 2023, 7:59:36 PM1/28/23
to CasADi
Hi Horea,

Thank you again for your diagnosis. For your last comment, indeed I only set the path for the header (the casadi.hpp file) and not for binaries. But how can I set the path for the binaries as well? So far (in Ubuntu 20.04) I have tried adding the entire casadi folder into the include path, here is what my .json file for VS Code looks like:

{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/tong/.local/lib/python3.8/site-packages/casadi/include",
"/home/tong/.local/lib/python3.8/site-packages/casadi"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

But when compiling a simplest casadi code I have the following error:

src/simplest.cpp:1:10: fatal error: casadi/casadi.hpp: No such file or directory
    1 | #include <casadi/casadi.hpp>
      |          ^~~~~~~~~~~~~~~~~~~
compilation terminated.

Seems like the compiler cannot find such a directory. I installed CasADi 3.5.5 using 'pip install casadi'.

Thank you for reading. I am again out of ideas. So I appreciate any hint or suggestion.
Reply all
Reply to author
Forward
0 new messages