Search code examples
ccmakelibxml2

How to get c source dir not cmake compile dir


I have article.html in src for test libxml, I hope read it with relative path '.article', but in clion, cmake change current dir to "/home/roroco/.CLion12/system/cmake/generated/c9a7a4c5/c9a7a4c5/Debug0", My question is: is there get_current_source_dir()?

update here is my 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);
};

here is my files tree

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

here is my CMakeLists.txt


Solution

  • While the proper way to solve this is to put article.html into /home/roroco/.CLion12/system/cmake/generated/b321980/b321980/Debug, here is what you can do:

    In CMakeLists.txt:

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

    And in ex.c:

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