Robert Lewis
Robert Lewis

Reputation: 1907

CMake can't see source file, can't find #includes

My first attempt to use CLion (Mac) and CMake. My project root folder is /ref. It is marked as sources root. All source code folders inside /ref are marked as library root except /ref/src, where my main.c resides.

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.6)
project(miniFEPosit)
set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES /src/main.cpp)            # Add main.cpp file of project root directory
add_executable(miniFEPosit ${SOURCE_FILES})

When I try to build, CLion says "Cannot find source file: /src/main.cpp". I get the same message if I try just main.cpp.

Also, in the main.cpp file, all the #include statements say "cannot find" the reference—even obvious ones like #include <iostream>.

Separately, can anyone recommend a good place to learn about CMake?

Upvotes: 0

Views: 4903

Answers (2)

flaviodesousa
flaviodesousa

Reputation: 7835

Change /src to just src. The '/' makes it look for the file directory on the root dir.

set(SOURCE_FILES src/main.cpp)

Regarding the include files you need to add a include_directories() clause containing your project directories that contain needed header files. This lets cmake to provide them correctly to make and to the compiler.

Upvotes: 2

Robert Lewis
Robert Lewis

Reputation: 1907

Here is the revised CMakeLists.txt file that fixed the issues:

cmake_minimum_required(VERSION 3.6)         # CMake version check
project(miniFEPosit)                        # Create project 
set(CMAKE_CXX_STANDARD 11)                  # Enable c++11 standard

set(SOURCE_FILES src/main.cpp)              # Add src/main.cpp file of project root directory as source file

include_directories(basic fem posit utils)  # subdirectories of project root /ref 

add_executable(miniFEPosit ${SOURCE_FILES}) # Add executable target

The key to finding main.cpp was eliminating the slash before src/main.cpp, which evidently causes it to expect an absolute path. And marking directories as "library root" in CLion doesn't cause them to be searched; you must have an include_directories(...) command. (The CLion documentation seems to indicate that marking a directory as library root should work, but perhaps CMake and the CMakeLists.txt file override this feature—any insight appreciated.)

Upvotes: 1

Related Questions