简体   繁体   English

使用 g++ 包含来自不同文件夹的 header 文件

[英]Include header file from different folder using g++

I was trying to install FLTK library and saw header files that uses different directory.我试图安装 FLTK 库并看到使用不同目录的 header 文件。 So I tried to test it out as:所以我尝试将其测试为:

\\check.cpp in D:\C++\
#include <factorial.h> // self made header file in folder D:\C++\link\
#include <iostream>
int main(){
\\ blah blah and call to factorial function from factorial.h
}

Do NOTE: link folder contains 1. string.exe(simple exe) 2. factorial.h.. I am trying to compile check.cpp from D:\C++\ using cygwin as:请注意:链接文件夹包含 1. string.exe(simple exe) 2. factorial.h .. 我正在尝试使用 cygwin 从 D:\C++\ 编译 check.cpp:

g++ this.cpp -ID:\C++\link -o done.exe 

When there is only factorial.h in link folder(D:\C++\link), the compilation successfully completes and produce working executable.当链接文件夹(D:\C++\link)中只有 factorial.h 时,编译成功完成并生成工作可执行文件。 But when there is another file string.exe in the same directory, the g++ tries to take exe file as input... The exact compile error is as:但是当同一目录下还有另一个文件 string.exe 时,g++ 尝试将 exe 文件作为输入...确切的编译错误为:

In file included from 

/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/locale_classes.h:40:0,
                     from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/ios_base.h:41,
                     from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/ios:42,
                     from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/ostream:38,
                     from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/iostream:39,
                     from check.cpp:2:
    D:\C++\link/string:1:3: error: stray ‘\220’ in program
     MZ□       □□  □       @                                   □   □ □ □!□L□!This program cannot be run in DOS mode.

       ^
    D:\C++\link/string:1:5: error: stray ‘\3’ in program
     MZ□       □□  □       @                                   □   □ □ □!□L□!This program cannot be run in DOS mode.
         ^
    D:\C++\link/string:1:9: error: stray ‘\4’ in program
     MZ□       □□  □       @                                   □   □ □ □!□L□!This program cannot be run in DOS mode. 

As far as I can guess, using -ID:\C++\link\ tried to take string.exe as input or something like that...据我猜测,使用 -ID:\C++\link\ 试图将 string.exe 作为输入或类似的东西......

Have you tried using slashes instead of backslashes?您是否尝试过使用斜杠而不是反斜杠? / instead of \ ? /而不是\

I think what you see is the compiler trying to include string.exe when calling #include <string> somewhere in <iostream> .我认为您看到的是编译器在<iostream>某处调用#include <string>时试图包含 string.exe 。

Also calling the folder 'link' is kind of confusing, 'include' would be a better name.也称文件夹“链接”有点令人困惑,“包含”会是一个更好的名字。

It looks like you are using MinGW - this may help you: MinGW include path howto看起来您正在使用 MinGW - 这可能会对您有所帮助: MinGW include path howto

[Can't comment so as an answer.] [无法评论作为答案。]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM