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

Time:09-27

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

CodePudding user response:

I had another file containing a main function in the same folder. Renaming the second one solved the issue and now it links fine.


I found the problem by compiling in release mode which gave the following error message

...
tmp.cpp:(.text.startup 0x0): multiple definition of `main'; CMakeFiles/tmp.dir/iss5839.cpp.o:iss5839.cpp:(.text.startup 0x0): first defined here
collect2: error: ld returned 1 exit status
...

CodePudding user response:

The following example (cgal_ex.cpp) works for me:

#include <iostream>
#include <CGAL/Simple_cartesian.h>
typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_2 Point_2;

int main()
{
  Point_2 p(0,0), q(3,4);
  std::cout << "p = " << p << std::endl;
  std::cout << "q = " << q.x() << " " << q.y() << std::endl;
  std::cout << "sqdist(p,q) = "
            << CGAL::squared_distance(p,q) << std::endl;
  return 0;
}
g   cgal_ex.cpp; a.out
p = 0 0
q = 3 4
sqdist(p,q) = 25

Reference: https://doc.cgal.org/latest/Manual/tutorial_hello_world.html

  • Related