Search code examples
c++cgal

Using a basic function of CGAL, e.g. `square`


I am starting to work my way through CGAL and have a question about the Algebraic foundations package.

How do you use the square function defined here?

I tried the following

// tmp.cpp
#include <CGAL/number_utils.h>

int main() {
  double dist = 0.002;
  auto sq_dist = CGAL::square(dist);
  return 0;
}

The compiler throws the following error message

$ make tmp
Consolidate compiler generated dependencies of target tmp
[ 14%] Building CXX object CMakeFiles/tmp.dir/tmp.cpp.o
In file included from /home/USER/info/cgal/tmp/tmp.cpp:1:
/home/USER/info/cgal/Algebraic_foundations/include/CGAL/number_utils.h: In instantiation of ‘typename CGAL::Algebraic_structure_traits<Type_>::Square::result_type CGAL::square(const AS&) [with AS = double; typename CGAL::Algebraic_structure_traits<Type_>::Square::result_type = CGAL::Null_tag; typename CGAL::Algebraic_structure_traits<Type_>::Square = CGAL::Null_functor]’:
/home/USER/info/cgal/tmp/tmp.cpp:5:30:   required from here
/home/USER/info/cgal/Algebraic_foundations/include/CGAL/number_utils.h:71:18: error: no match for call to ‘(CGAL::Algebraic_structure_traits<double>::Square {aka CGAL::Null_functor}) (const double&)’
   71 |     return square( x );
      |            ~~~~~~^~~~~
make[3]: *** [CMakeFiles/tmp.dir/build.make:146: CMakeFiles/tmp.dir/tmp.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/tmp.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/tmp.dir/rule] Error 2
make: *** [Makefile:124: tmp] Error 2

It is mentioned in this github issue.


Update: Following the comment by @MarcGlisse I changed the header include to

// tmp.cpp
#include <CGAL/number_utils.h>
#include <CGAL/basic.h>

int main() {
  double dist = 0.002;
  auto sq_dist = CGAL::square(dist);
  return 0;
}

Compilation runs through smoothly but linking throws new errors

$ make tmp
[ 14%] Building CXX object CMakeFiles/tmp.dir/tmp.cpp.o
[ 28%] Linking CXX executable tmp
/usr/bin/ld: CMakeFiles/tmp.dir/tmp.cpp.o: in function `main':
/home/USER/info/cgal/STL_Extension/include/CGAL/exceptions.h:88: multiple definition of `main'; /usr/bin/ld: DWARF error: section .debug_str is larger than its filesize! (0x32f0bc2 vs 0x2447b28)
/usr/bin/ld: DWARF error: section .debug_str is larger than its filesize! (0x32f0bc2 vs 0x2447b28)
/usr/bin/ld: DWARF error: section .debug_str is larger than its filesize! (0x32f0bc2 vs 0x2447b28)
...

The referenced line STL_Extension/include/CGAL/exceptions.h:88 can be found here.


Update: I found the issue (see my answer) but it doesnt make sense to me. Why am I not able to have multiple main functions in different files in the same folder?

I built the following way

cd ${CGAL_DIR}/tmp
cgal_create_CMakeLists -s tmp
cmake -DCMAKE_BUILD_TYPE=Debug .
make tmp

Solution

  • The issue arises because of the -s tmp argument.

    The solution is to call it in either of the following formats

    cgal_create_CMakeLists tmp
    cgal_create_CMakeLists 
    

    The documentation gives an explanation

    $ cgal_create_CMakeLists -h
    Usage: cgal_create_CMakeLists [-s source] ...
    ...
      -s source If this parameter is given the script will create one single executable for 'source' with all source files; otherwise it creates one executable for each main'ed source.
    ...
    

    There is no reasonable way to create one executable for files containing multiple main functions.