包括Clion中的目录 [英] Including directories in Clion

查看:1457
本文介绍了包括Clion中的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我想要包含一个位于我的项目之外的目录时,我会使用 -I somedir 标志。这一次,我想做的是有这样的层次结构:

Whenever I wanted to include a directory that was located outside of my project with Clion I would use the -I somedir flag. This time however, what I want to do is to have a hierarchy like this:

/project
   CMakeLists.txt
   /src
      /Graph
         Graph.h
         Graph.cpp
      /Dijkstra
         Dijkstra.h
         Dijstra.cpp

我希望我的代码在 / src 目录中。不仅如此,而且,例如,在文件 Dijkstra.h 内,我想包括Graph.h像这样: #include Graph / Graph.h ,而不是这样: #include../ Graph / Graph.h

I want my code in a /src directory. And not only that, but also, for example, inside the file Dijkstra.h I want to include the Graph.h like this: #include "Graph/Graph.h and not like this: #include "../Graph/Graph.h.

如果我只添加一个 -I src 标志,那么如果我在 Dijkstra.h 文件,我想要包括 Graph.h ,我必须写 #include../Graph/Graph.h

If I only add an -I src flag, then if I am inside the Dijkstra.h file and I wanted to include Graph.h, I would have to write #include "../Graph/Graph.h, which is not what I want.

所以我也试着添加 INCLUDE_DIRECTORIES(src) 。这解决了上面的问题,但是当试图编译时,我有一个链接器错误未定义的引用...

So I tried to also add INCLUDE_DIRECTORIES(src). That fixed the problem above, however when tried to compiled, I got a linker error undefined reference to....

所以我试着逐一添加这些文件,如下:

So I tried adding the files one by one like this:

set(SOURCE_FILES
        src/Dijkstra/Dijkstra.h
        src/Dijkstra/Dijkstra.cpp
        src/Graph/Graph.h
        src/Graph/Graph.cpp)
add_executable(someprojectname ${SOURCE_FILES})

并带回了以前的问题,其中我不得不包括这样的文件: #include../ Graph / Graph.h

and that brought back the previous problem, where I had to include the files like this: #include "../Graph/Graph.h".

如何正确地获得我想要的行为?

How can I do this properly to get the behavior I want ?

推荐答案

命令 INCLUDE_DIRECTORIES 不添加任何源文件进行编译!

而是,此命令定义搜索头文件的目录。

Instead, this command defines directories for search header files.

您需要列出所有 / strong>中的文件 $ add_executable()调用

You need to list all source files in add_executable() call in any case:

include_directories(src)
set(SOURCE_FILES
    src/Dijkstra/Dijkstra.cpp
    src/Graph/Graph.cpp)
add_executable(someprojectname ${SOURCE_FILES})

这篇关于包括Clion中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆