Matlab Coder Stand Alone Executable Linking Third Party Libs

I have the following file example from the coder page,
function core()
fprintf("Hello World!!\n");
coder.updateBuildInfo('addCompileFlags','-lcurl');
coder.updateBuildInfo('addSourceFiles','foo.c');
y = 0;
y = coder.ceval('foo', 10, 20);
fprintf("%f\n", y);
end
That I am trying to link libcurl into final executable. I supply the following main.c file,
#include <stdio.h>
#include <stdlib.h>
#include "core_initialize.h"
#include "core.h"
#include "core_terminate.h"
int main()
{
core_initialize();
core();
core_terminate();
return 0;
}
with the following foo.c
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
double foo(double in1, double in2)
{
return in1 + in2;
}
int call(){
CURL *curl;
CURLcode res;
curl = curl_easy_init();
}
It only uses curl_easy_init just to test linking it does do anything usefull.
Then use the following commands to build the standalone executable on linux (libcurl is present on the system along with gcc i can compile programs linking curl just fine from bash).
cfg = coder.config('exe');
cfg.CustomSource = 'main.c';
cfg.CustomInclude = ['"',pwd,'"'];
codegen -config cfg -report core
At the final linking stage it fails with undefined reference to curl easy init. If i edit the make file manually and add -lcurl to SYSTEM_LIBS variable along with -lm it compiles. I can see the curl flag passed to all compile targets except the one that links all togerher. It calls c compiler on all files but last step is ran with g++ instead of gcc even though all my files are C files.
What is the proper way to pass thirt party libraries to matlab coder?

Answers (1)

Hi Nurullah,
-lcurl should be a linker flag instead of compiler flag.
So change the line to
coder.updateBuildInfo('addLinkFlags','-lcurl');
should fix the problem.
~Anakin

4 Comments

Hi Anakin,
I did try this before posting I forgot to mention it, what happens is it does add the flag. But it adds it before the objects files not after so gcc does not link it. AFAIK -lcurl should be after the .o files however flag is placed before the .o files which causes gcc error. From what I can tell from the generated make file -lcurl should be placed like a SYSTEM_LIB along side math library (If a manually edit the generated makefile and add -lcurl to system libs it compiles fine). But I can not find how to set those flags.
Hi Nurullah,
You can use
coder.updateBuildInfo('addSysLibs', 'curl');
This is actually not well documented, but should do the thing for you.
Most linkers today are smart linkers and usually not very picky about flag order. Could you let me know which OS and GCC version you are using?
Thanks
~Anakin
Funny, I just ran into the same issue. Its for the GXX tool chain on linux 64 bit x86, and linux arm64 nvidia.
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0

Sign in to comment.

Products

Release

R2019b

Asked:

on 15 Oct 2019

Commented:

on 21 Mar 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!