daj
daj

Reputation: 31

cmake: add directory to be searched for header files

I am writing c++ code to run tests on an arduino project I've written. In order to run it, I need it to include mockups of a couple libraries (the original libraries don't work in c++). The mockups are in my src directory, along with the tests in main.cpp. The files that rely on the mockups are in the parent directory of src, and I cannot modify them to refer to src without breaking their ability to run when uploaded to an arduino. I also cannot add the mockups or main.cpp to the parent directory, as this would interfere with the operation of the arduino code.

So, I need to add the child directory with the mockups to the directories that are searched when compiling the files in the parent directory.

Directory Outline:

parent
    forTest.h
    forTest.cpp
    src
        parson.h
        parson.c
        String.h
        String.cpp
        main.cpp
        CMakeLists.txt
    build

In this case, main.cpp has "#include ../forTest.cpp", and forTest.cpp has "#include parson.h" and "#include String.h"

I am currently running the following code in CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project(makeTest)

include_directories(../ )

set (makeTest_VERSION_MAJOR 1)
set (makeTest_VERSION_MINOR 0)

add_executable(makeTest main.cpp ../forTest.cpp String.cpp parson.c)

I then build it in the build directory from the command line with

cmake -G "Unix Makefiles" ../src
make

The cmake command resolves successfully, but the make command hits "fatal error: parson.h not found" when building forTest.cpp

How can I resolve this?

edit: my apologies if this has an obvious answer, I'm very new to Cmake

edit the second: using the changes suggested by Gergely Nyiri and changing #include to #include "/usr/include/string.h" resolves several errors, but introduces a new error : "implicit instantiation of undefined template" during the make step.

It refers to the following code in by string.h mockup file:

#ifndef STRING_H
#define STRING_H

#include "/usr/include/string.h"
using namespace std;

class String {
private:
    std::string thisString;
    char chars[];
...
#endif

which returns the error:

In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:61:
/Users/douglas/Desktop/parts/makeHolder/testMake/src/string.h:9:14: error: 
implicit instantiation of undefined template 'std::__1::basic_string<char,
  std::__1::char_traits<char>, std::__1::allocator<char> >'
    std::string thisString; 

This is followed by:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iosfwd:193:32: note: 
    template is declared here
    class _LIBCPP_TEMPLATE_VIS basic_string;

Upvotes: 0

Views: 1378

Answers (1)

Gergely Nyiri
Gergely Nyiri

Reputation: 345

First of all, I would propose to put CMakeLists.txt in your source root. Then:

cmake_minimum_required (VERSION 2.6)
project(makeTest)

include_directories(src)

set (makeTest_VERSION_MAJOR 1)
set (makeTest_VERSION_MINOR 0)

add_executable(makeTest src/main.cpp forTest.cpp src/String.cpp src/parson.c)

Configure & build:

cd build
cmake ../
make

Even better if you use target_include_directories instead of include_directories:

..
add_executable(makeTest src/main.cpp forTest.cpp src/String.cpp src/parson.c)
target_include_directories(makeTest PRIVATE src)

Upvotes: 1

Related Questions