Search code examples
c++cmakeclionclang++

C++ project can't read file when building with CMake


So I have written a C++ project which reads a file into a string.

//func to read file into string
std::string readFile( const std::string& fileName )
{
    std::ifstream ifs( fileName );
    return std::string((std::istreambuf_iterator<char>(ifs)),
        (std::istreambuf_iterator<char>()));
}

//func call in main
std::string text = readFile("test.txt");

This is the code I use for this.
I am using CLion to write my code. I changed the standard compiler to clang++. When I run the main.cpp directly in CLion the program can't read the file. There is no error code, but when I am debugging the program it says that the parameter fileName has an incomplete type.
When I compile the main.cpp in the terminal with

clang++ -std=c++14 main.cpp -o histogram

and then run it, it can read the file and the program works as it should do.
Why does it work in the terminal but not in CLion?


Solution

  • As said in the comments, CMake created a subfolder in which the output file was located. The test.txt file was in the source folder and therefore out of range. When I changed the working directory to the source folder the program worked.
    Thank you for the quick help.