0

I have a generated .zip file that contains generated C code from a Simulink model. The goal is to compile this code in a shared library that I can use from a .NET Core backend.

I have to compile it for both Windows as Unix systems because most of the developers are using Windows, while the server runs on Unix (and myself runs on osx).

The zip has the following structure:

    optimizationModel_grt_rtw
    R2019b
       extern/include
       rtw/c/src/
       simulink/include 

I also have a "script" that uses the model inside the _grt_rtw folder. The "script" contains one function that uses the model.

It seems I can compile it succesfully as follows:

    gcc -I ./optimlib/R2019b/extern/include/ \
    -I ./optimlib/R2019b/simulink/include/ \
    -I ./optimlib/optimizationModel4_grt_rtw/ \
    -I ./optimlib/R2019b/rtw/c/src/ \
    -c -fpic src/optimumCalc.c \
    -o out/optimumCalc.o

But when I try to link I get a whole lot of errors of unused references for x86_64 architecture. I'm pretty sure everything is there in the code.

Errorlogs:

Undefined symbols for architecture x86_64:
  "_optimizationModel4_B", referenced from:
      _calcOptimum in optimumCalc.o
  "_optimizationModel4_U", referenced from:
      _calcOptimum in optimumCalc.o
  "_optimizationModel4_Y", referenced from:
      _calcOptimum in optimumCalc.o
  "_optimizationModel4_initialize", referenced from:
      _calcOptimum in optimumCalc.o
  "_optimizationModel4_step", referenced from:
      _calcOptimum in optimumCalc.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The linker command I use is the following: gcc -dynamiclib out/optimumCalc.o -o out/optimlib.dylib

I assume there is something I'm missing with the linker command, but I can't figure it out. I also tried passing the -I parameters I passed with the first command.

I really can't find any documentation on how to compile this using gcc on systems that don't have the MATLAB/Simulink environments installed. If anyone can point me to the right direction or see what I'm missing in the linker command?

Thanks!

JC97
  • 1,530
  • 2
  • 23
  • 44
  • 1
    "I get a whole lot of errors". Would be probably helpful to know what these errors are. – Guillaume Petitjean Aug 27 '20 at 09:34
  • I can add the exact log I'm getting, but you still wouldn't understand anything without looking at the source code (which is private source code btw). "unused reference errors" is all I can give you. Added the logs anyway. @GuillaumePetitjean – JC97 Aug 27 '20 at 10:13
  • the error log you pasted mentions `undefined symbols errors` meaning that something is expected to be there (e.g. `_optimizationModel4_B` ) but it isn't. check the code and verify that the `_optimizationModel4_B` is indeed there, and then add that file to your includes or reorganize the code. – Shark Aug 27 '20 at 11:31
  • @JC97 Don't really understand your latest comment: Shark suggested a pretty logical explanation but you don't want to follow it ? A symbol is not found. did you check where this symbol is defined and declared ? Is the source file where it is defined included in the make ? Is the path to the header file where it is declared included in the make ? – Guillaume Petitjean Aug 27 '20 at 12:30
  • Yes, what I meant was I checked every reference stated in the log and double checked everything was in the source code. So I did that before posting the question. I also asked a guy with 15+ years of experience in C/C++, but he doesn't have a lot of time. He was pretty sure it had something to do with the linker command, since the code is fine. Not using a makefile, just the plain gcc command. Maybe it wasn't clear in the question (my bad), but it just seemed you were assuming I didn't check anything before posting a question here by immediatly downvoting. (1/2) – JC97 Aug 27 '20 at 12:39
  • I can also can compile by using the -I directives to give the paths where the header/source files are located, only when trying to create a shared/dynamic library, it goes wrong. Also because it works on a plc environment, I can be 100% sure it's not the header/source files that are wrong, right? If you have any pointers on how I can start by making a cmake file (because there isn't any right now) or where I have to start looking on which linker parameters I have to use to make sure I have everything I'm glad to hear it. (2/2) – JC97 Aug 27 '20 at 12:39

0 Answers0