CasADi in C++

3,869 views
Skip to first unread message

Gil Serrancolí

unread,
Jun 15, 2016, 12:18:22 PM6/15/16
to CasADi
Hi,

We would like to run optimizations in CasADi (direct collocation) using data coming from an API which is built in C++ (OpenSim/Simbody API). I would like to know your reccommendations on how to transform the data from C++ for use in CasADi, would it be possible?
Right now, we are using CasADi through the Matlab interface in Windows 7, but the ultimate goal is to run everything in C++ (in Windows).

Thank you very much in advance.

Best regards,

Gil

Joel Andersson

unread,
Jun 15, 2016, 1:05:30 PM6/15/16
to CasADi
Hi Gil,

So you want to translate your direct collocation implementation from CasADi-MATLAB to CasADi-C++? Or you want to pass data available in C++ to your CasADi-MATLAB code?

Joel

Gil Serrancolí

unread,
Jun 15, 2016, 1:23:24 PM6/15/16
to CasADi
Hi Joel,

Thanks for the quick answer. The ultimate goal would be to work in C++ for all parts. So, CasADi-C++ would be preferable. Would that be possible?

Thank you very much.

Best regards,

Gil

El dimecres, 15 juny de 2016 19:05:30 UTC+2, Joel Andersson va escriure:

Joel Andersson

unread,
Jun 15, 2016, 2:22:10 PM6/15/16
to CasADi
Hi Gil,

CasADi is written in C++ so anything you can do in CasADi-MATLAB, you can also do in CasADi-C++. There is less documentation for C++, but you will find some pointers in the user guide (http://casadi.sourceforge.net/v3.0.1/users_guide/casadi-users_guide.pdf), in particular in chapter 9 about differences in syntax between different languages.

You can also have a look at some of the examples that are available both for C++ and MATLAB. For example, compare

to


Note that if you're planning to use CasADi from C++, you could either compile CasADi from sources yourself, but this might be a bit tedious, especially for Windows. You also need to compile any third-party tool that you plant to use (e.g. IPOPT). If you don't want to compile from sources, note that the CasADi builds for both Python and MATLAB are distributed with the C++ header files and libraries. To locate the CasADi libraries, the following CMake file can be useful: https://github.com/casadi/casadi/blob/develop/cmake/FindCASADI.cmake

I hope that helps,
Joel

Peter Listov

unread,
Sep 15, 2016, 9:39:38 AM9/15/16
to CasADi
Hello Joel,

I experience some problems when trying to run the simplest CasADi example in C++. I have downloaded and installed CasADi-3.0.0-rc1 binary package for Linux and placed 'FindCASADI.cmake' where Cmake can locate it (would it be a good idea to distribute this file with the package?).  Cmake does not show any issues:

-- Found CasADi include dir: /usr/include;/usr/include/casadi

-- Found CasADi libs: /usr/lib/libcasadi_nlpsol_ipopt.so;/usr/lib/libcasadi_csparse.so;/usr/lib/libcasadi_tinyxml.so;/usr/lib/libcasadi.so

-- Configuring done

-- Generating done


But I cannot compile even this line of code:

#include "casadi/casadi.hpp"

int main()
{
    casadi::SX sym = casadi::SX("sym",4);
    std::cout << sym << "\n";

    return 0;
}

The error reads:

/home/listov/catkin_ws/src/test_package/src/casadi_test.cpp:5:40: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
     casadi::SX sym = casadi::SX("sym",4);
                                        ^
In file included from /usr/include/casadi/core/sx/sx_elem.hpp:33:0,
                 from /usr/include/casadi/core/core.hpp:30,
                 from /usr/include/casadi/casadi.hpp:29,
                 from /home/listov/catkin_ws/src/test_package/src/casadi_test.cpp:1:
/usr/include/casadi/core/matrix.hpp:100:5: error:   initializing argument 1 of ‘casadi::Matrix<T>::Matrix(int, int) [with Scalar = casadi::SXElem]’ [-fpermissive]
     Matrix(int nrow, int ncol);


Would you have an idea, what can be wrong here? Thanks in advance.


Best regards,
Peter

Joris Gillis

unread,
Sep 15, 2016, 10:22:13 AM9/15/16
to CasADi
Dear Peter,

While using the binaries for C++ compiling is certainly possible, you may encounter some pitfalls.
In this case, it appears you are simply making a syntax error though.
What about
casadi::SX sym = casadi::SX::sym("sym",4);


Best regards,
  Joris

Peter Listov

unread,
Sep 16, 2016, 4:59:01 AM9/16/16
to CasADi
Hello Joris,

That's a silly mistake indeed. Thanks for help!

Best regards,
Peter

Mohamed W. Mehrez

unread,
May 18, 2021, 1:36:59 PM5/18/21
to CasADi
Hi,

I am trying to run the simplest c++ example using Casadi on ubuntu. I downloaded the latest python package for linux and tried to use the FindCasadi.cmake file mentioned above. However, I am still unsuccessful getting my code compiled. My cmake simply couldn't find the casadi path or libraries. I was wondering what is the proper folder/file structure of a casadi c++ example if I rely only on the library distributed with casadi for python.

Thanks a lot in advance!
Mohamed

Joel Andersson

unread,
May 21, 2021, 4:19:00 PM5/21/21
to CasADi
You should not need to use any FindCasadi.cmake file at all. There should be a "casadi-config.cmake" installed during installation that you can use with find_package in your CMake project. There is also a pkg-config file created that could also be used.

Joel

Iuro Nascimento

unread,
Sep 16, 2021, 6:55:43 PM9/16/21
to CasADi
Hi,

I tried  to compile a simple code as in 

https://gist.github.com/iurobpn/51e931b3a4d0171a95102806b3234910

The c++ code only uses a

SX x = SX::sym("x");

inside main() with

using namespace casadi;

before main. The compilation fails either with cmake or make with the same error. The compilation (the gist has the Makefile, CMakeLists.txt and the c++ code) is

g++ -Lpath-to-lib -Ipath-to-header -o teste test_casadi.cpp -lcasadi

The error is an undefined reference:

/tmp/cc3GZwPR.o: In function `casadi::GenericMatrix<casadi::Matrix<casadi::SXElem> >::sym(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, casadi::Sparsity const&)':
test_casadi.cpp:(.text._ZN6casadi13GenericMatrixINS_6MatrixINS_6SXElemEEEE3symERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_8SparsityE[_ZN6casadi13GenericMatrixINS_6MatrixINS_6SXElemEEEE3symERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_8SparsityE]+0x36): undefined reference to `casadi::Matrix<casadi::SXElem>::_sym(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, casadi::Sparsity const&)'
collect2: error: ld returned 1 exit status
Makefile:9: recipe for target 'teste' failed
make: *** [teste] Error 1

I'm on Xubuntu 18.04, I've tested g++ 11, 10 and I've tested with both Matlab and python versions of casadi(3.5.5). If anyone can help me I thank you guys in advance.

Kind Regards,

Iuro Nascimento

Joel Andersson

unread,
Sep 21, 2021, 9:51:07 PM9/21/21
to CasADi
Looks like some library incompatibility between the CasADi binary you're using and your compiler. Are you missing a "-std=c++11" flag?
You can also try to compile CasADi from sources - it's pretty straightforward on Ubuntu. It can get tricky if you're using a lot of plugins however.

Joel

Horea Alexandru Caramizaru

unread,
Dec 1, 2021, 8:34:31 AM12/1/21
to CasADi
Hi All,

Were you able to fix it? I am having the same problem too and I would prefer not to recompile casadi as I am using multiple plugins.

Best,
Horea C.

Shubham Singh

unread,
Aug 22, 2022, 11:34:00 AM8/22/22
to CasADi
Hi everyone
I am also trying a very basic casadi C++ example, and I get the error "reference to ‘casadi’ is ambiguous". Did anyone get a similar error? I have a hunch that I am missing including the casadi package while compiling my script.
Thanks

Tong Zhao

unread,
Jan 22, 2023, 7:27:45 PM1/22/23
to CasADi
Is it possible to use Casadi in C++ programs without building from the source? I assume one would need to download either the MATLAB or the Python API version of the CasADi binary package, then somehow link the binary package to the actual C++ program that uses CasADi.

I'd appreciate if someone can point to an example of this (C++ program utilizing a pre-built CasADi binary package) if possible?

Thanks,
Tong

Mingyuan Tao

unread,
Mar 27, 2023, 6:03:03 AM3/27/23
to CasADi
Hi Tong,

Hope it helps.

Best,
Mingyuan
Reply all
Reply to author
Forward
0 new messages