Bi Wang Bi Wang - 2 months ago 27
C++ Question

Header errors in CLion

I met an annoying problem in Clion which is that there are always header errors in my project.

Here is my

CmakeLists.txt
:

cmake_minimum_required(VERSION 3.6)
project(geometry)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(PROJECT_SOURCE_DIR geo)
set(CMAKE_INCLUDE_CURRENT_DIR geo)
include_directories("${PROJECT_BINARY_DIR}")
include_directories(${CMAKE_BINARY_DIR}/geo/)

set(SOURCE_FILES

geo/Circle.cpp
geo/Circle.h
geo/geo.h
geo/ICircle.cpp
geo/ICircle.h
geo/IPoint.cpp
geo/IPoint.h
geo/IRectangle.cpp
geo/IRectangle.h
geo/IShape.cpp
geo/IShape.h
geo/IText.cpp
geo/IText.h
geo/Point.cpp
geo/Point.h
geo/Rectangle.cpp
geo/Rectangle.h
geo/Shape.cpp
geo/Shape.h
geo/Text.cpp
geo/Text.h
owl/IThing.cpp
owl/IThing.h
owl/owl.h
owl/Thing.cpp
owl/Thing.h
rdfs/IResource.cpp
rdfs/IResource.h
rdfs/rdfs.h
rdfs/Resource.cpp
rdfs/Resource.h
AllInOne.cpp
example.cpp
geometry.owl)

add_executable(geometry ${SOURCE_FILES})


Here is the
example.cpp
which is also the main cpp:

#include <autordf/Factory.h>

#include <geo/geo.h>

using namespace geo;


int main(int argc, char ** argv) {
autordf::Factory f;
f.addNamespacePrefix("geo", "http://example.org/geometry#");
autordf::Object::setFactory(&f);

geo::Rectangle r("http://example.org/myfancyrectangle");

// Set one of my rectangle coordinates - the long way
geo::Point tl;
tl.setX(1.0);
tl.setY(2.0);
r.setTopLeft(tl);

// Set one of my rectangle coordinates - the short way
r.setBottomRight(geo::Point().setX(11).setY(12));

// Set result of my work
f.saveToFile(stdout, "turtle");
}


It works perfectly in the terminal but in Clion the result is:

Scanning dependencies of target geometry
[ 5%] Building CXX object CMakeFiles/geometry.dir/geo/Circle.cpp.o
[ 11%] Building CXX object CMakeFiles/geometry.dir/geo/ICircle.cpp.o
[ 17%] Building CXX object CMakeFiles/geometry.dir/geo/IPoint.cpp.o
/home/burning/projects/autordf_projects/geometry/geo/Circle.cpp:2:24: fatal error: geo/Circle.h: No such file or directory
#include <geo/Circle.h>
^
compilation terminated.
/home/burning/projects/autordf_projects/geometry/geo/ICircle.cpp:2:25: fatal error: geo/ICircle.h: No such file or directory
#include <geo/ICircle.h>
^
compilation terminated.
/home/burning/projects/autordf_projects/geometry/geo/IPoint.cpp:2:24: fatal error: geo/IPoint.h: No such file or directory
#include <geo/IPoint.h>
^
compilation terminated.
[ 23%] Building CXX object CMakeFiles/geometry.dir/geo/IRectangle.cpp.o
/home/burning/projects/autordf_projects/geometry/geo/IRectangle.cpp:2:28: fatal error: geo/IRectangle.h: No such file or directory
#include <geo/IRectangle.h>
^
compilation terminated.
make[3]: *** [CMakeFiles/geometry.dir/geo/ICircle.cpp.o] Error 1
make[3]: *** Waiting for unfinished jobs....
make[3]: *** [CMakeFiles/geometry.dir/geo/Circle.cpp.o] Error 1
make[3]: *** [CMakeFiles/geometry.dir/geo/IPoint.cpp.o] Error 1
make[3]: *** [CMakeFiles/geometry.dir/geo/IRectangle.cpp.o] Error 1
make[2]: *** [CMakeFiles/geometry.dir/all] Error 2
make[1]: *** [CMakeFiles/geometry.dir/rule] Error 2
make: *** [geometry] Error 2


One of the header files in
geo/
(e.g.
Circle.cpp
) looks like:

/** @cond internal */
#include <geo/Circle.h>

// This is auto generated code by AutoRDF, do not edit !

namespace geo {

Circle::Circle(const std::string& iri) : autordf::Object(iri, ICircle::TYPEIRI) {
}

Circle::Circle(const Object& other) : autordf::Object(other) {
}

std::vector<Circle> Circle::find() {
return findHelper<Circle>(ICircle::TYPEIRI);
}

}

/** @endcond internal */


So, the problem is that I cannot include the other headers in the header file. As you can see above the error would be:

/home/burning/projects/autordf_projects/geometry/geo/Circle.cpp:2:24: fatal error: geo/Circle.h: No such file or directory
#include <geo/Circle.h>
^
compilation terminated.


Could someone help me with that?

Answer

As mentioned in the comments, you should use:

include_directories("${PROJECT_SOURCE_DIR}")

Instead of:

include_directories("${PROJECT_BINARY_DIR}")

From the documentation of PROJECT_SOURCE_DIR, we have that:

Top level source directory for the current project.

From the documentation of PROJECT_BINARY_DIR, we have that:

Full path to build directory for project.

Thus, unless you allow in-source builds for your projects, it's likely that you want to use the source directory as an include directory, not the build directory.