Search code examples
c++c++11lambdacmakecgal

C++ Vector Sort method fails to compile, returns expected expression


I'm starting to work with cgal and I have just downloaded some sample code to start practicing. The problem is that when I try to compile the code, it returns:

ConvexHull.cpp:275:40: error: expected expression
sort(points.begin(), points.end(), [p] (Point_2 a, Point_2 b) -> int {
                                  ^
1 error generated.
make[2]: *** [CMakeFiles/exec.e.dir/ConvexHull.cpp.o] Error 1
make[1]: *** [CMakeFiles/exec.e.dir/all] Error 2
make: *** [all] Error 2

When I take a look at the code, everything seems ok:

Point_2 p = *(polygon.bottom_vertex());

sort(points.begin(), points.end(), [p] (Point_2 a, Point_2 b) -> int {
    Vector_2 v1(a, p);
    Vector_2 v2(b, p);
    
    return v1.direction() <= v2.direction();
});

Is this really wrong ? Am I using the wrong compiler (It's using by default Clang 3.1.0) ? Did I miss anything ?

Any help is greatly appreciated

By the way: I'm using OSX Lion to develop, and the only thing I've installed is cgal(using homebrew) and its dependencies. To compile I run:

cd path/to/folder
cgal_create_CMakeLists -c Qt4:Core:GMP:MPFR:Boost -s exec.e
cmake -DCGAL_DIR=$HOME/CGAL-4.2 -DCMAKE_BUILD_TYPE=Debug
make

EDIT

I managed to install gcc-4.8 and g++-4.8 using homebrew. Than I ran the cmake command like this:

cmake -DCGAL_DIR=$HOME/CGAL-4.2 -DCMAKE_CXX_COMPILER=g++-4.8 -DCMAKE_CC_COMPILER=gcc-4.8

Didn't even need to specify the c++11 flag. It generated the executable file, but when I run it using ./exec.e it segfaults Segmentation fault: 11. I am very frustrated at how hard this is turning out to be.

EDIT 2

I gave up using the MacOS. I was able to easily compile and run every required library and CGALon a linux using GCC-4.7.


Solution

  • The code example that you showed has a C++11 language feature called lambda expressions. Most compiler do not run in C++11 mode by default. To explicitly let them do so, put inside your CMakeLists.txt

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

    and regenerate, rebuild and rerun everything to get your program to work correctly.