使用 CGAL 的基本功能,例如`正方形`
Using a basic function of CGAL, e.g. `square`
我正开始学习 CGAL,对 Algebraic foundations 包有疑问。
如何使用定义的square
函数here?
我尝试了以下方法
// tmp.cpp
#include <CGAL/number_utils.h>
int main() {
double dist = 0.002;
auto sq_dist = CGAL::square(dist);
return 0;
}
编译器抛出如下错误信息
$ 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
在这个github issue中提到了。
更新:根据@MarcGlisse 的评论,我将 header 包含更改为
// 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;
}
编译运行顺利,但链接出现新错误
$ 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)
...
可以找到引用的行 STL_Extension/include/CGAL/exceptions.h:88
here。
更新:我发现了这个问题(见我的回答),但它对我来说没有意义。为什么我无法在同一文件夹的不同文件中使用多个 main
函数?
我是按照下面的方式搭建的
cd ${CGAL_DIR}/tmp
cgal_create_CMakeLists -s tmp
cmake -DCMAKE_BUILD_TYPE=Debug .
make tmp
以下示例 (cgal_ex.cpp) 对我有用:
#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
参考:[https://doc.cgal.org/latest/Manual/tutorial_hello_world.html][1]
为同一目录中具有 main 的两个文件添加 Makefile 示例:
MODULES=cgal_ex cgal_ex1
%: %.cpp
g++ -O2 -o $@ $<
strip $@
%: %.c
gcc -o $@ $<
strip $@
clean:
-rm -rf $(MODULES)
all: $(MODULES)
用法示例:
make all -f Makefile.cgal
g++ -O2 -o cgal_ex cgal_ex.cpp
strip cgal_ex
g++ -O2 -o cgal_ex1 cgal_ex1.cpp
strip cgal_ex1
cgal_create_CMakeLists 的用法示例。
资料来源:https://github.com/CGAL/releases/blob/main/scripts/cgal_create_CMakeLists
用法示例:
- 创建新目录,其中只有 3 个文件:
cgal_create_CMakeLists cgal_ex.cpp cgal_ex1.cpp
- 运行 以下命令:
cmake $PWD
make
结果:
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using header-only CGAL
-- Targetting Unix Makefiles
-- Using /usr/bin/c++ compiler.
-- Found GMP: /usr/lib/x86_64-linux-gnu/libgmp.so
-- Found MPFR: /usr/lib/x86_64-linux-gnu/libmpfr.so
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found suitable version "1.71.0", minimum required is "1.48")
-- Boost include dirs: /usr/include
-- Boost libraries:
-- Using gcc version 4 or later. Adding -frounding-math
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found version "1.71.0")
CMake Warning at /usr/lib/x86_64-linux-gnu/cmake/CGAL/CGAL_enable_end_of_configuration_hook.cmake:99 (message):
=======================================================================
Call Stack (most recent call first):
CMakeLists.txt:9999 (CGAL_run_at_the_end_of_configuration)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/<username>/cgal
Scanning dependencies of target cgal_ex1
[ 25%] Building CXX object CMakeFiles/cgal_ex1.dir/cgal_ex1.cpp.o
[ 50%] Linking CXX executable cgal_ex1
[ 50%] Built target cgal_ex1
Scanning dependencies of target cgal_ex
[ 75%] Building CXX object CMakeFiles/cgal_ex.dir/cgal_ex.cpp.o
[100%] Linking CXX executable cgal_ex
[100%] Built target cgal_ex
[1]: https://doc.cgal.org/latest/Manual/tutorial_hello_world.html
[2]: https://github.com/CGAL/releases/blob/main/scripts/cgal_create_CMakeLists
问题的出现是因为 -s tmp
参数。
解决方法是以下两种格式之一调用
cgal_create_CMakeLists tmp
cgal_create_CMakeLists
文档给出了解释
$ 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.
...
没有合理的方法为包含多个 main
函数的文件创建一个 可执行文件。
我正开始学习 CGAL,对 Algebraic foundations 包有疑问。
如何使用定义的square
函数here?
我尝试了以下方法
// tmp.cpp
#include <CGAL/number_utils.h>
int main() {
double dist = 0.002;
auto sq_dist = CGAL::square(dist);
return 0;
}
编译器抛出如下错误信息
$ 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
在这个github issue中提到了。
更新:根据@MarcGlisse 的评论,我将 header 包含更改为
// 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;
}
编译运行顺利,但链接出现新错误
$ 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)
...
可以找到引用的行 STL_Extension/include/CGAL/exceptions.h:88
here。
更新:我发现了这个问题(见我的回答),但它对我来说没有意义。为什么我无法在同一文件夹的不同文件中使用多个 main
函数?
我是按照下面的方式搭建的
cd ${CGAL_DIR}/tmp
cgal_create_CMakeLists -s tmp
cmake -DCMAKE_BUILD_TYPE=Debug .
make tmp
以下示例 (cgal_ex.cpp) 对我有用:
#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
参考:[https://doc.cgal.org/latest/Manual/tutorial_hello_world.html][1]
为同一目录中具有 main 的两个文件添加 Makefile 示例:
MODULES=cgal_ex cgal_ex1
%: %.cpp
g++ -O2 -o $@ $<
strip $@
%: %.c
gcc -o $@ $<
strip $@
clean:
-rm -rf $(MODULES)
all: $(MODULES)
用法示例:
make all -f Makefile.cgal
g++ -O2 -o cgal_ex cgal_ex.cpp
strip cgal_ex
g++ -O2 -o cgal_ex1 cgal_ex1.cpp
strip cgal_ex1
cgal_create_CMakeLists 的用法示例。 资料来源:https://github.com/CGAL/releases/blob/main/scripts/cgal_create_CMakeLists
用法示例:
- 创建新目录,其中只有 3 个文件:
cgal_create_CMakeLists cgal_ex.cpp cgal_ex1.cpp
- 运行 以下命令:
cmake $PWD
make
结果:
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using header-only CGAL
-- Targetting Unix Makefiles
-- Using /usr/bin/c++ compiler.
-- Found GMP: /usr/lib/x86_64-linux-gnu/libgmp.so
-- Found MPFR: /usr/lib/x86_64-linux-gnu/libmpfr.so
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found suitable version "1.71.0", minimum required is "1.48")
-- Boost include dirs: /usr/include
-- Boost libraries:
-- Using gcc version 4 or later. Adding -frounding-math
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found version "1.71.0")
CMake Warning at /usr/lib/x86_64-linux-gnu/cmake/CGAL/CGAL_enable_end_of_configuration_hook.cmake:99 (message):
=======================================================================
Call Stack (most recent call first):
CMakeLists.txt:9999 (CGAL_run_at_the_end_of_configuration)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/<username>/cgal
Scanning dependencies of target cgal_ex1
[ 25%] Building CXX object CMakeFiles/cgal_ex1.dir/cgal_ex1.cpp.o
[ 50%] Linking CXX executable cgal_ex1
[ 50%] Built target cgal_ex1
Scanning dependencies of target cgal_ex
[ 75%] Building CXX object CMakeFiles/cgal_ex.dir/cgal_ex.cpp.o
[100%] Linking CXX executable cgal_ex
[100%] Built target cgal_ex
[1]: https://doc.cgal.org/latest/Manual/tutorial_hello_world.html
[2]: https://github.com/CGAL/releases/blob/main/scripts/cgal_create_CMakeLists
问题的出现是因为 -s tmp
参数。
解决方法是以下两种格式之一调用
cgal_create_CMakeLists tmp
cgal_create_CMakeLists
文档给出了解释
$ 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.
...
没有合理的方法为包含多个 main
函数的文件创建一个 可执行文件。