V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
proxytoworld
V2EX  ›  问与答

c++新手 求问 c++项目头文件检索问题

  •  
  •   proxytoworld · 2021-12-07 17:10:29 +08:00 · 711 次点击
    这是一个创建于 878 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我有这样结构的项目(windows 系统+vscode+cmaketools+mingw)

    |-bin
    |
    |-include|
    |	 \includes.h
    |
    |-src|
    |    \main.cc
    |    |CmakeLists.txt
    |
    |-CmakeLists.txt
    
    

    我在 main.cc 中使用 #include "include\includes.h" 包含头文件

    但是我使用在src目录下使用命令g++ -shared -o ../bin/test.exe -I ..include .\main.cc编译时提示include\includes.h不存在

    我在根目录的 CmakeLists.txt 中通过add_subdirectory(src)包含 src 的 Cmake File 。 在 src\CmakeLists.txt 使用了如下代码

    
    # 设置 exe 源代码变量
    
    
    add_library(curldll SHARED dllmain.cc)
    target_compile_definitions(curldll PRIVATE EXPORT)
    target_include_directories(curldll PRIVATE ../include)
    

    在该项目中使用 cmake 编译,还是提示找不到include\includes.h文件,我只能通过include "..\include\includes.h"来包含头文件

    但是我看 GitHub 上面的其他项目都是通过#include "include\includes.h"包含,很奇怪该怎么做才不会报错。

    7 条回复    2021-12-07 22:47:25 +08:00
    Nasei
        1
    Nasei  
       2021-12-07 17:22:00 +08:00
    直接 include "includes.h" 就可以把...
    proxytoworld
        2
    proxytoworld  
    OP
       2021-12-07 17:42:52 +08:00
    @Nasei 谢谢大佬编译没问题,但是 vscode 会标红提示找不到头文件
    xipuxiaoyehua
        3
    xipuxiaoyehua  
       2021-12-07 17:47:55 +08:00
    @proxytoworld vscode 要设置 include path
    hhjuteman
        4
    hhjuteman  
       2021-12-07 17:56:17 +08:00
    target_include_directories(PATH) <=> g++ -I${PATH}
    如果用的 vscode c++插件,里面应该有 compiler_command.json 相关的选项。

    插件根据 compiler command 的数据进行符号索引
    推荐使用 clangd
    proxytoworld
        5
    proxytoworld  
    OP
       2021-12-07 18:44:53 +08:00
    @xipuxiaoyehua OK 了 谢谢大佬
    proxytoworld
        6
    proxytoworld  
    OP
       2021-12-07 18:45:14 +08:00
    @hhjuteman 我试试
    TimothyT
        7
    TimothyT  
       2021-12-07 22:47:25 +08:00
    g++是不是少打了个分隔符? -I ../include

    cmake 的话在 target_include_directories 下一行把 INCLUDE_DIRECTORIES 打印出来看看添加成功了没试试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   820 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:18 · PVG 06:18 · LAX 15:18 · JFK 18:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.