15

I am trying to include a library file named libmathematica.a in gcc so it gets linked in the executable example.

I attempt to do this with gcc main.c libmathematica.a -o example

Note: I have to do this with gcc, as ld won't link it properly with the correct system libraries

But I get: fatal error: mathematica.h: No such file or directory ,which is odd because mathematica.h is in the library.

Can you help?

Corwin Mcknight
  • 302
  • 1
  • 2
  • 9
  • See also: [c - gcc Can't Find a Included Header - Stack Overflow](https://stackoverflow.com/questions/2139920/gcc-cant-find-a-included-header) – user202729 Oct 24 '21 at 00:45

3 Answers3

22

A header file cannot be in the library. It has to be present at a certain location and you have to specify that location with the -I compiler flag:

gcc -I/path/to/mathematica/include main.c libmathematica.a -o example

If the header file is in the directory where the main.c is or in a subdirectory, then be sure that you use quotes and not angle brackets in the #include directive.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
5

The issue would be in your source file. If the mathematica.h is in the system includes directory then you would use #include <mathematica.h> and if it was in some local directory then you would use something like #include "libs/mathematica.h".

  • I don't follow... See main.c isn't part of mathematica.a, its a external program. I'm trying to link mathematica to example to run code from mathematica – Corwin Mcknight May 05 '12 at 17:47
  • Where is the mathematica.h file located? do something like find / -name "*mathematica.h" . If it is in the main includes such as /usr/include then you can use #include otherwise it should be placed in the same directory as your main.c and use #include "mathematica.h". – Timothy Swartz May 05 '12 at 17:53
1

Try adding to the gcc call - an option like -I/Full/Path/To/The/Directory/Where/the/desired/header/resides

For example: gcc -I/usr/include/mathematica -lmathematica -o example main.c

Yotam
  • 808
  • 8
  • 10