CLion doesn't resolve headers from external li

2019-04-20 05:29发布

问题:

Some time ago I started a big header library in C++1x using XCode. The current layout of the library is () something like (partial output from ls -R sponf)

sponf/sponf:
ancestors        sponf.h                sponf_utilities.h
categories       sponf_children.h       utilities
children         sponf_macros.h           

sponf/sponf/ancestors:
function.h       meter.h        set.h                    simulation.h

sponf/sponf/categories:
free_space.h     prng.h         random_distribution.h    series.h

sponf/sponf/children:
distributions    histogram.h    random                   simulations
meters           numeric        series                   spaces

sponf/sponf/children/distributions:
arcsine_der.h    exponential.h
box_muller.h     uniform.h

sponf/sponf/children/meters:
accumulator.h    timer.h

#... other subdirs of 'children' ...

sponf/sponf/utilities:
common_math.h    limits.h       string_const.h

#... other directories ...

I wanted to port this project to CLion, which seems a really good IDE (based on the similar AndroidStudio IDE) but I'm getting some troubles.

Small test program

I tried this small program as a test:

#include <iostream>
#include <sponf/sponf.h>

using namespace std;

int main() {
    using space = sponf::spaces::euclidean_free_space<double, 3>;
    sponf::simulations::random_walk<space> rw;

    rw.step(1);

    std::cout << rw.position.value << std::endl;

    return 0;
}

The program compiles and runs fine. However, CLion does not recognize the spaces namespace (declared in one of the children files), nor the simulations namespace; they are both marked red and I cannot inspect their content, nor navigate to their definitions by -clicking, etc. etc...

Relevant parts of the library

Looking in "sponf.h" we find

#ifndef sponf_h
#define sponf_h

/* The classes below are exported */
#pragma GCC visibility push(default)

// include some of the standard library files
// ...

#include <Eigen/Eigen>

#include "sponf_macros.h"

#include "sponf_utilities.h"
#include "sponf_children.h"

#pragma GCC visibility pop

#endif

while in "sponf_children.h" (which is located at the top level, next to "sponf.h") we find

#ifndef sponf_locp_sponf_children_h
#define sponf_locp_sponf_children_h

namespace sponf {

// include some of the children
// ...

#include "children/spaces/euclidean_free_space.h"
#include "children/simulations/random_walk.h"

// include remaining children
// ...

}

#endif

Each "child" header will then include its corresponding "ancestor" or "category" header (which defines the superclass of the "child" itself).

The reaction of CLion

Despite the autocompletition prediction, which easily finds all the subdirectories and the headers, all the include directives in this last file get marked red and -clicking on any of them leads to a popup message

Cannot find declaration to go to

while the right ribbon of the editor signal many errors like

',' or ) expected

) expected

Declarator expected

Expecting type

Missing ;

Unexpected symbol

which are not the same for each include statement (each generates from 2 to all of these errors).

On the other hand, CLion is perfectly able to find all Eigen headers, which have pretty much the same structure!

I have put both libs in /opt/local/include and changed CMakeLists.txt accordingly

cmake_minimum_required(VERSION 2.8.4)
project(sponf)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

include_directories(/opt/local/include/sponf /opt/local/include/eigen3)

set(SOURCE_FILES main.cpp)
add_executable(sponf ${SOURCE_FILES})

Why can't CLion properly parse the project structure? XCode, after having included /opt/local/include/sponf and /opt/local/include/eigen3 in the HEADER_SEARCH_PATHS env. variable of the project, is able to find any header while compiling the same exact program.

Is there anything else I need to know? Am I doing it wrong or is it that CLion isn't that mature yet and this is just a sorry bug? This is my first approach to the CLion and the CMake toolchain, so any kind of information about it will be greatly appreciated!

Sorry for the very long question, I didn't manage to shrink it further... Thanks in advance guys, see you soon!

回答1:

Here what I did in windows using cigwin64. I wanted to use Eigen library include in my project. Eigen library is places in /usr/include/eigen then edited CMakeLists.txt and add

  include_directories("/usr/include/eigen") 

into it. Now CLion can find all source files in eigen lib. May be this what you wanted too.



回答2:

Downgrade to Clion 2016.1.4 fixes the problem