2015-11-02 47 views
0

我有src文章中的article.html測試libxml,我希望閱讀它的相對路徑'.article',但在clion中,cmake將當前目錄更改爲「/ home/roroco /.CLion12/system/cmake/generated/c9a7a4c5/c9a7a4c5/Debug0「,我的問題是:是否有get_current_source_dir()如何獲得c源代碼不cmake編譯目錄

更新這裏是我的ex.c:

#include <unistd.h> 
#include "stdio.h" 

int main(int argc, char **argv) { 
    puts(get_current_dir_name()); 
// /home/roroco/.CLion12/system/cmake/generated/b321980/b321980/Debug 
    FILE *f = fopen('article.html', "r"); 
    fseek(f, 0, SEEK_END); 
// Signal: SIGSEGV (Segmentation fault) 
    return (0); 
}; 

這裏是我在這裏的文件樹

- ex.c 
- CMakeLists.txt 
- article.html 

是我CMakeLists.txt

+0

這似乎並非是一個C語言的問題,因此當你刪除C得到更好的反應標籤。請提供您嘗試實施的解決方案的一些示例。 – Erik

+0

@Erik,這是關於C,我更新我的代碼 – nwaicaethi

+0

非常好。 SIGSEGV發生是因爲你沒有檢查NULL的返回值''fopen'''。如果您使用絕對路徑而不是相對路徑,則會獲得更多成功。 – Erik

回答

1

雖然適當的方式來解決,這是對把article.html寫入/home/roroco/.CLion12/system/cmake/generated/b321980/b321980/Debug,這裏是你可以做的事情:

在的CMakeLists.txt:

add_definitions("-DSOURCE_PATH=\"${CMAKE_SOURCE_PATH}\"") 

而且在ex.c:

fopen(SOURCE_PATH "article.html", "r"); 
+0

thx,它適合我的情況 – nwaicaethi