简体   繁体   中英

compilation error when including directory containing headers

I have a directory maths which is a library that is comprised solely of header files. I am trying to compile my program by running the following command in my home directory:

g++ -I ../maths prog1.cpp prog2.cpp test.cpp -o et -lboost_date_time -lgsl -lgslcblas

but I get the following compilation error:

prog1.cpp:4:23: fatal error: maths/Dense: No such file or directory
compilation terminated.
prog2.cpp:6:23: fatal error: maths/Dense: No such file or directory
compilation terminated.

maths is located in the same directory(ie my home directory) as the .cpp files and I am running the compilation line from my home as well.

prog1.cpp and prog2.cpp have the following headers #include<maths/Dense> on lines 4 and 6 respectively, hence I am getting the error.

how do I fix it.

You can either change your include path to -I.. or your includes to #include <Dense>

Wait, if maths is in the same directory as your source files and that is your current directory, you can either change your include path to -I. or your includes to #include "Dense"

maths is located in the same directory(ie my home directory) as the .cpp files

Your include path is given as -I ../maths . You need -I ./maths – or simpler, -I maths since maths is a subdirectory of the current directory , not of the parent directory. Right?

Then in your C++ file, use #include <Dense> . If you want to use #include <maths/Dense> you need to adapt the include path. However, using -I. may lead to massive problems 1 , I strongly advise against this.

Instead, it's common practice to have an include subdirectory that is included. So your folder structure should preferably look as follows:

./
+ include/
| + maths/
|   + Dense
|
+ your_file.cpp

Then use -I include , and in your C++ file, #include <maths/Dense> .


1) Consider what happens if you've got a file ./map.cpp from which you generate an executable called ./map . As soon as you use #include <map> anywhere in your code, this will try to include ./map instead of the map standard header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM