1

Good day,

I have a file that I'm trying to compile and within it has an #include to a statically linked binary.

#!/bin/bash

g++ -Wall -std=c++17 Message.cpp ../textmagic-rest-cpp/lib/libtextmagic.a

I am getting the following error: fatal error: libtextmagic.h: No such file or directory

The relative path that I provided is correct under the assumption that the current working directory is the directory in which the script is called/ran. I might be linking the binary incorrectly and I've searched around the internet but the other posts/resources did not help me.

Note that the script is run in the same directory as Message.cpp.

  • 1
    You need to give the `-I /path/to/the/include` option to `g++`, so that the path + the file name in the `#include` refers correctly to the file. Libraries are usually linked with the `-L` and `-l` flags. – walnut Nov 09 '19 at 22:19
  • 1
    Possible duplicate of [How to include header files in GCC search path?](https://stackoverflow.com/questions/973146/how-to-include-header-files-in-gcc-search-path) – walnut Nov 09 '19 at 22:21

2 Answers2

2

g++ has the -I and -L flags that do that for you. Your flag will look like this: -I/ThePathToYourHeaders and -L/ThePathToYourLib. I don't know if g++ supports relative paths there but absolut paths are guaranteed to work there.

Also you probably need to add a linker flag. For your project it will be -ltextmagic. It is just the name of the .a file you want to link with, without the lib in front of the filename.

user11914177
  • 885
  • 11
  • 33
  • 1
    Thank you, so would something like ... -I../textmagic-rest-cpp/include/textmagic.h -L../textmagic-rest-cpp/lib -ltextmagic.h be on the right path (assuming relative paths work)? -I would be the path to the include and -L would be the path to the .a library file? – Chris Turgeon Nov 09 '19 at 22:32
  • 2
    @ChrisTurgeon No, the file name of the header should not be included in the `-I` option, only the path (assuming you refer to it only by file name in the `#include`). The `-l` option should refer to the library name without `.a` and without leading `lib`, i.e. `-lextmagic`, not the header name. – walnut Nov 09 '19 at 22:36
  • 1
    So first with the `-I` and `-L` flags it is common to use the path that contains the library not each file alone, this really is keeping your compile command short. So for you it should look like this: ` -I../textmagic-rest-cpp/include` and` -L../textmagic-rest-cpp/lib`. With the linker flags, you only need to link to each `.a` file, not the headers. Also as I said, because your `.a` file is named `libtextmagic.a` you need to get rid of the `lib` in the beginning and the `.a` in the end, resulting in `-ltextmagic`. Also there are no paths here, just the filename. – user11914177 Nov 09 '19 at 22:37
  • 1
    @uneven_mark Thank you very much guys! This solved my problem, and I learned a bunch about g++ and this library stuff. Cheers! – Chris Turgeon Nov 09 '19 at 22:43
  • 1
    @ChrisTurgeon Btw. these flags should work for pretty much any compiler. – user11914177 Nov 09 '19 at 22:45
1

The #include directive needs to "read" the header file you give it as argument, and that is not included in the static library.

You can either include using a relative path to the source file or pass the location of the header file to the compiler using the -I argument.

Paul92
  • 8,827
  • 1
  • 23
  • 37