![](/img/trans.png)
[英]C language: error: ‘O_DIRECTORY’ undeclared and accessing files in directory and sub-directory(without using opendir())
[英]c language including files without directory?
包含 /usr/include/test/head1.h 文件我應該寫#include "test/head1.h"
但我怎么能只寫#include "head1.h"
來包含這個文件。
編譯器會在/usr/include
等常用位置查找文件。
如果您知道編譯器已經在/usr/include
中查找包含文件,那么實現此目的的一種方法是:
#include "test/head1.h"
如果您打算不再包含來自/usr/include/test
的文件並且您知道沒有其他包含目錄具有類似名稱的文件以避免沖突,這將是一個好方法。
其他方法是在編譯時為編譯器提供目錄提示:
gcc -I/usr/include/test <other-options> yourcode.c
玩得開心寫 C!
我上面的答案是正確的,但我只是想添加一些建議,如果您使用的是 CMake,則可以讓#include
工作。
如果您使用的是 CMake,則可以在 CMakeLists.txt 中添加直到 header 的路徑,如下所示:
target_include_directories(<projectname> PUBLIC path/to/headers)
,您可以添加任意數量的路徑,以空格分隔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.