win10+vs2019+CGAL5.3.1+Boost1.72+QT配置及例子

1、背景

最近研究三维的东西比较多,里面很多用到几何的内容,而最强大的几何库是CGAL,而CGAL库是依赖boost的,同时一些可视化的东西是依赖QT,QT是可选的,因为可视化在项目实际使用一般不是必要的,但是在调试时比较有帮助。

环境:我的大环境是win10+vs2019。

2、编译Boost、安装QT及相关配置

(1)boost:

win10下boost安装的两种方式,及cmake编译boost代码例子_luozhichengaichenlei的博客-CSDN博客一、编译与配置Boost(1)boost的下载地址:这里我下载的是boost_1_72_0.zip(170 MB)Boost Version Historyhttps://www.boost.org/users/history/Boost C++ Libraries - Browse /boost-binaries at SourceForge.netFree peer-reviewed portable C++ source librarieshttps://sourceforge.net/prohttps://blog.csdn.net/luozhichengaichenlei/article/details/122490831(2)QT-5.12:(支持离线安装)

(更新,QT-5.12最高vs版本库是msvc_2017,这和我用的VS2019不符,因此这一小节可以跳过,直接看下面的安装QT-5.15,不过这里我做了一些测试可以供大家参考,但是没有做足够的测试,测试1:我在另一台仅安装VS2017的机器上,安装了boost-1.72-vc141,QT-5.12和CGAL,运行后面的CGAL-Qt代码,多次运行没问题;测试2:我的笔记本上安装了VS2017和VS2019,我安装boost-1.72-vc142,QT-5.12和CGAL,运行后面的CGAL-Qt代码,仅仅在第一次运行可以正常显示,第二次开始就不能显示,可能我的电脑的库台乱了导致的)

根据CGAL 5.3.1 - Manual: Using CGAL on Windows (with Visual C++)介绍的,我去下载了qt-opensource-windows-x86-5.12.10.exe,可以离线安装QT,下载地址为:中国科技大学镜像或腾讯镜像。

Index of /qtproject/http://mirrors.ustc.edu.cn/qtproject/​​​​​​Index of /qt/archive/qt/https://mirrors.cloud.tencent.com/qt/archive/qt/安装时候,登录QT账号:[email protected],QT账号密码:luan1994***,然后勾选按照下图(虽然勾选的是VS2017,虽然我后面用的IDE和编译器都是VS2019,但是不影响使用)

 安装完成后将“XXX\QT\5.12.10\msvc2017_64\bin”添加到环境变量中。

win10+vs2019+CGAL5.3.1+Boost1.72+QT配置及例子_第1张图片

(3)QT-5.15:(仅支持在线安装)

(更新,即使安装了msvc_2019库,我的笔记本也没能够拜托运行不显示的问题,然后把库直接复制到其他的装有VS2019电脑上面,却能够完好多次运行没问题,认为是我电脑的库太乱导致的)

QT-5.15开始能够安装msvc_2019库,但是仅仅支持在线安装,如果直接用QT官网的方式安装,那么你总会因为网络原因装不下来。于是参考博客里面的方法;QT6在线安装下载速度慢的解决办法,QT6,QT5.15.1,QT5.15.0及旧版本都支持_cnchu的博客-CSDN博客_qt安装慢QT5.15在线安装下载速度慢的解决办法下载加速使用清华源地址安装代理软件Fiddler5打开安装QT在线安装软件使用QT安装器可以愉快地在线下载QT下载加速使用清华源地址QT软件清华源地址: https://mirrors.tuna.tsinghua.edu.cn/qt安装代理软件Fiddler51、安装并打开Fiddler5,软件自动启用代理。(Fiddler5软件百度搜索)2、在Fiddler软件下方快速执行的栏里输入并回车:urlreplace download.qt.io mihttps://blog.csdn.net/cnchu/article/details/109049845(1)首先是下载抓包工具Fiddler,直接到官网下英文版的软件即可,然后傻瓜式安装;Download Fiddler Web Debugging Tool for Free by TelerikDownload and install Fiddler Classic web debugging tool. Watch a quick tutorial to get started.https://www.telerik.com/download/fiddler

(2)打开Fiddler.exe,然后按照博客一样,在Fiddler软件下方快速执行的栏里输入并回车,只不过我发现清华源和其他源都比较慢,于是我换成了腾讯的源:

urlreplace download.qt.io mirrors.cloud.tencent.com/qt/

(3)下载QT的在线安装软件,qt-unified-windows-x86-online.exe

Index of /qt/official_releases/online_installers/https://mirrors.cloud.tencent.com/qt/official_releases/online_installers/(4)双击打开qt-unified-windows-x86-online.exe,然后傻瓜式的一步一步安装即可,这里我勾选的内容和离线版本勾选的基本一致,就是MSVC_2019_64-bit、Source、QtCreator再加一个QtCreator Debug工具。

(5)设置环境变量。

3、编译CGAL5.3.1

(0)CGAL5.3.1是head-only库,不编译直接cmake是可以的(没做更多测试),但是编译也是可以的。

(1)下载,下载地址为:Releases · CGAL/cgal · GitHubhttps://github.com/CGAL/cgal/releases

我是下载了“CGAL-5.3.1.zip”和“GMP and MPFR libraries, for Windows 64bits”(GMP and MPFR也是CGAL的依赖库)

(2)解压CGAL-5.3.1.zip,将CGAL-5.3.1-win64-auxiliary-libraries-gmp-mpfr.zip解压到...\CGAL-5.3.1\auxiliary\gmp目录下;

(3)cmake-gui先conf一遍,然后修改CMAKE_INSTALL_PREFIX为你自己install的目录,最后conf+gene+open proj打开工程,分别在debug和release下生成INSTALL;

(4)编译完成后,将...\CGAL-5.3.1\auxiliary\gmp复制到install目录下,目录结构保持一致;
    (根据FindMPFR.cmake中${CGAL_INSTALLATION_PACKAGE_DIR}/auxiliary/gmp/include而进行修改,否则后续用cmakelist使用CGAL时候会出问题)

(5)添加环境变量(这里有大坑):将...\auxiliary\gmp\lib所在的目录放在环境变量中,并且把这个环境变量尽量上移,这是因为在我们的win10中,可能存在别的地方也有libgmp-10.dll这个文件,并且版本不同,这就会让我们在运行代码的时候出现错误,这个坑了我一天才找到原因。

最后,我们可以看到,我们编译完后没有生成lib和dll,这是因为在CGAL-5.3.1/INSTALL.md中已经说明了,CGAL现在是header-only的库,类似Eigen一样把源码全部放在头文件里。

4、CGAL简单例子(CGAL-only、CGAL-boost、CGAL-Qt)

这里我的cmake版本为3.18.5,cmakelist里面需要手动设置CGAL_DIR和Qt5_DIR。

(1)CGAL-only

CMakeLists.txt为:

cmake_minimum_required(VERSION 3.1...3.20)

#set (CMAKE_CXX_STANDARD 14)
add_compile_options("/std:c++17")

project(CGAL-only)

if(NOT POLICY CMP0070 AND POLICY CMP0053)
  # Only set CMP0053 to OLD with CMake<3.10, otherwise there is a warning.
  cmake_policy(SET CMP0053 OLD)
endif()
if(POLICY CMP0071)
  cmake_policy(SET CMP0071 NEW)
endif()

#CGAL
set(CGAL_DIR D:/Lzc/vs2019/project/3Dmeasure/src-pclbo/CGAL/install/lib/cmake/CGAL)
find_package(CGAL COMPONENTS Core)
include(${CGAL_USE_FILE})
include_directories(${CGAL_INSTALLATION_PACKAGE_DIR}/auxiliary/gmp/include)

FILE(GLOB CPP_FILE0 ${PROJECT_SOURCE_DIR}/src/*.cpp)
add_executable(CGAL_test ${CPP_FILE0})

main.cpp为:

#include 
#include 

typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef K::Segment_2 Segment_2;
typedef K::Line_2 Line_2;
typedef K::Intersect_2 Intersect_2;

int main()
{
    Segment_2 seg(Point_2(0, 0), Point_2(2, 2));
    Line_2 lin(1, -1, 0);
    CGAL::cpp11::result_of::type
        result = intersection(seg, lin);
    if (result) {
        if (const Segment_2* s = boost::get(&*result)) {
            std::cout << *s << std::endl;
        }
        else {
            const Point_2* p = boost::get(&*result);
            std::cout << *p << std::endl;
        }
    }
    return 0;
}

(2)CGAL-boost

CMakeLists.txt为:

cmake_minimum_required(VERSION 3.1...3.20)

#set (CMAKE_CXX_STANDARD 14)
add_compile_options("/std:c++17")

project(CGAL-boost)

if(NOT POLICY CMP0070 AND POLICY CMP0053)
  # Only set CMP0053 to OLD with CMake<3.10, otherwise there is a warning.
  cmake_policy(SET CMP0053 OLD)
endif()
if(POLICY CMP0071)
  cmake_policy(SET CMP0071 NEW)
endif()

#CGAL+Boost
set(CGAL_DIR D:/Lzc/vs2019/project/3Dmeasure/src-pclbo/CGAL/install/lib/cmake/CGAL)
set(CGAL_Boost_USE_STATIC_LIBS ON)
find_package(CGAL COMPONENTS Core)
find_package(Boost COMPONENTS filesystem)
include(${CGAL_USE_FILE})
include_directories(${CGAL_INSTALLATION_PACKAGE_DIR}/auxiliary/gmp/include)

FILE(GLOB CPP_FILE0 ${PROJECT_SOURCE_DIR}/src/*.cpp)
add_executable(CGAL_test ${CPP_FILE0})
target_link_libraries(CGAL_test ${Boost_LIBRARIES})

main.cpp为:

#include 
#include 

#include 

typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef K::Segment_2 Segment_2;
typedef K::Line_2 Line_2;
typedef K::Intersect_2 Intersect_2;

int main()
{
    boost::filesystem::path MyPath1{ "./a/b/c.jpg" };
    std::string MyPath2 = MyPath1.string();
    std::cout << MyPath2 << std::endl;

    Segment_2 seg(Point_2(0, 0), Point_2(2, 2));
    Line_2 lin(1, -1, 0);
    CGAL::cpp11::result_of::type
        result = intersection(seg, lin);
    if (result) {
        if (const Segment_2* s = boost::get(&*result)) {
            std::cout << *s << std::endl;
        }
        else {
            const Point_2* p = boost::get(&*result);
            std::cout << *p << std::endl;
        }
    }
    return 0;
}

(3)CGAL-Qt

CMakeLists.txt为:

cmake_minimum_required(VERSION 3.1...3.20)

#set (CMAKE_CXX_STANDARD 14)
add_compile_options("/std:c++17")

project(CGAL-Qt)

if(NOT POLICY CMP0070 AND POLICY CMP0053)
  # Only set CMP0053 to OLD with CMake<3.10, otherwise there is a warning.
  cmake_policy(SET CMP0053 OLD)
endif()
if(POLICY CMP0071)
  cmake_policy(SET CMP0071 NEW)
endif()

#CGAL+QT
set(CGAL_DIR D:/Lzc/vs2019/project/3Dmeasure/src-pclbo/CGAL/install/lib/cmake/CGAL)
set(Qt5_DIR D:/soft/QT5/5.15.2/msvc2019_64/lib/cmake/Qt5)
find_package(CGAL COMPONENTS Qt5 Core)
include(${CGAL_USE_FILE})
include_directories(${CGAL_INSTALLATION_PACKAGE_DIR}/auxiliary/gmp/include)
add_definitions(-DCGAL_USE_BASIC_VIEWER -DQT_NO_KEYWORDS)

FILE(GLOB CPP_FILE0 ${PROJECT_SOURCE_DIR}/src/*.cpp)
add_executable(CGAL_test ${CPP_FILE0})
target_link_libraries(CGAL_test PUBLIC CGAL::CGAL_Qt5)

main.cpp为:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 

typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point;
typedef CGAL::Creator_uniform_2 Creator;
typedef std::vector Vector;
typedef CGAL::Delaunay_triangulation_2 Delaunay;
typedef Delaunay::Vertex_handle Vertex_handle;

int main() {
	Vector points;
	points.reserve(10);

	CGAL::Random_points_in_disc_2 g(150.0);
	CGAL::cpp11::copy_n(g, 10, std::back_inserter(points));

	Delaunay dt;
	dt.insert(points.begin(), points.end());
	CGAL::draw(dt);

	return EXIT_SUCCESS;
}

你可能感兴趣的:(c++)