Using coolprop with C++

438 views
Skip to first unread message

Nathan Ricks

unread,
May 27, 2021, 12:29:56 PM5/27/21
to coolprop-users
Hey all,

I apologise if this is a simple question, however I haven't been able to find the solution around, and since I am not a C++ programmer by trade let's say.

I'm trying to utilise the CoolProp shared library within the C++ on Windows environment, however I'm running into issues with "undefined reference to ..." with whatever function I'm trying to call. I guess because it doesn't find the functions in the library?

Some context: Particular to my case, I'm using Matlab Coder to generate the C++ code (later will be combined with Simulink models, extensive Matlab libraries, etc, so not looking to change language unfortunately). When generating to C code and including CoolPropLib.h, and linking to CoolProp.lib with CoolProp.dll (available https://sourceforge.net/projects/coolprop/files/CoolProp/6.4.1/shared_library/Windows/64bit/), this can be compiled correctly. However, when I try to generate to C++ code I run into the previously mentioned compilation errors, even in simple examples not involving Matlab Coder.

So to try and be specific, my main question is: what are the specific files (headers, libraries, linking (?) libraries) that should be included when utilising a CoolProp shared library in C++ on Windows? 
- Is it CoolProp.h, CoolPropLib.h, or? 
- Is it CoolProp.dll, libCoolProp.dll, or?
- Is it the same .lib, .dll.a, or? 
- And how is a basic example compiled?

Is there a precompiled version around that should work for my use case? Or, I've successfully compiled the shared library myself using CMake and MinGW (g++ from mingw-w64/x86_64-8.1.0), but I just can't get it to be called correctly.

I appreciate any help you can give me!
Thanks a lot,
Kind regards,

Nathan

Ian Bell

unread,
May 29, 2021, 9:22:52 AM5/29/21
to coolpro...@googlegroups.com
Nathan,

You're talking about a lot of moving parts there.  First step is for you to be sure you understand how C++ works, and in particular how it pulls in dynamically linked libraries, both for compilation and at runtime.  Are you doing late linking of the shared library?  What does your calling code look like?  Without that, hard to help more.

If it were me, and I were really using C++, I would compile CoolProp to a static library and link that into the compilation unit so that there is no shared library runtime dependency.  That can be a real nightmare to debug sometimes.

The header file you need is CoolPropLib.h: https://github.com/CoolProp/CoolProp/blob/master/include/CoolPropLib.h Then you use CMake to build the static or shared library of CoolProp

I would build your C++ code with cmake as well to ensure that you can match compile flags, manage dependencies, and so on. There are static and shared library targets for CoolProp in cmake.

Also, on windows, I strongly recommend using visual studio.  Things "just work" and with mingw, that's a whole other layer of potential pitfalls.

Ian

--
You received this message because you are subscribed to the Google Groups "coolprop-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coolprop-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/coolprop-users/40e3e2b7-1195-48c9-8d5f-d69859183011n%40googlegroups.com.

Nathan Ricks

unread,
May 31, 2021, 3:04:35 AM5/31/21
to coolprop-users
Hi Ian,

Thanks for the tips despite the vague question! I decided to try down the static library route as you suggested and ended up getting that to work as expected with mingw. It turns out I think my problem was in one environment the compiler was defaulting to 32 bit, and in another 64 bit without me realising. After forcing bitness on building the static library I was able to get the project to compile and run successfully.

One final (hopefully) question, would the CMAKE_BUILD_TYPE=Release be the best option for fastest execution time? Only using the humid air properties at the moment if that changes anything.

Thanks a lot for your time and the project!
Kind regards,

Nathan

Ian Bell

unread,
Jun 1, 2021, 10:18:21 PM6/1/21
to coolpro...@googlegroups.com
Good to hear you got it working. 

Indeed,  release builds will always have the fastest runtime,  usually by a lot. 

Reply all
Reply to author
Forward
0 new messages