3
myproject/
  lib/
  src/
  project2/
    **CMakeLists.txt**
    test.cpp

I wrote CMakeLists.txt for compile 'test.cpp' file. I'd like to include /src because i should access src's file.

How could i include src file above? I tried like set(MORE_INCLUDE_DIR "-I${CMAKE_SOURCE_DIR}../../ but it's not working.

I should compile 'test.cpp' seperately please somebody help me.

Thanks in advance!

6
  • What's MORE_INCLUDE_DIR? You should call include_directories or target_include_directories. Commented May 23, 2018 at 7:36
  • sorry for abstract comments. MORE_INCLUDE_DIR used for set include_directories. Could i set include_directories at parent directory based on CMAKE_SOURCE DIRECTORY? Commented May 23, 2018 at 7:45
  • Instead of ${CMAKE_SOURCE_DIR}../../ you probably mean ${CMAKE_SOURCE_DIR}/../. BTW, using message() command you may print value of any variable, and check its value. Commented May 23, 2018 at 7:55
  • @Tsyvarev thank you for comment:) I already modified it and it couldn't find path. In this case, Am i wrong at other part? Commented May 23, 2018 at 8:03
  • It is difficult to say where you are wrong, because you show neither the code (CMakeLists.txt), which sets include directory, nor exact error message. According to explicit setting of -I option, it seems that you misunderstand CMake concepts. But again, without the code we can only guess. Commented May 23, 2018 at 8:07

1 Answer 1

8

In this case, cmake required absolute path not relative. Let's try call include_directories like this:

    get_filename_component(PARENT_DIR ../ ABSOLUTE)
    include_directories(${PARENT_DIR}/src)

It can help.

Sign up to request clarification or add additional context in comments.

2 Comments

In this example, how would your actual code properly include files from the src directory? I have a root folder, then in that root folder I have a "src" and a "test" dir. I'm writing a CMakeLists.txt that's in my "test" folder, but I want to include files from "src". If I add those 2 lines to the CMake file, but from within my test code I want to include a header from "src", how would I do it, if I can't put a ".." in the include path.
@RaleighL. Horrendous, isn't it? I'm sat here aghast at CMake's impenetrable mass of versions and weird syntaxes,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.