CMAKE-包含-VCGlib,-使用-VCG-库

VCG Library (简称VCG)是一个开源便携式C ++模板库,用于使用Opengl的三角形和四面体网格进行操作处理和显示。

问题描述

在 Cmake中添加该库除了使用 include_directories 包含头文件之外,还需要包含path-to-vcglib/wrap/ply/plylib.cpp ,否则在编译(确切说是连接)时就报错

====================[ Build | vcg_evt | Debug ]=================================
/opt/clion-2021.1.3/bin/cmake/linux/bin/cmake --build /home/era/workspace/test/evaluation/vcg-evt/cmake-build-debug --target vcg_evt -- -j 6
Scanning dependencies of target vcg_evt
[ 50%] Linking CXX executable vcg_evt
/usr/bin/ld: CMakeFiles/vcg_evt.dir/src/main.cpp.o: in function `vcg::tri::io::ExporterPLY<TMesh>::Save(TMesh const&, char const*, bool, vcg::tri::io::PlyInfo const&, bool (*)(int, char const*))':
/home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:211: undefined reference to `vcg::ply::PropDescriptor::stotypename() const'
/usr/bin/ld: /home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:218: undefined reference to `vcg::ply::PropDescriptor::stotypename() const'
/usr/bin/ld: /home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:218: undefined reference to `vcg::ply::PropDescriptor::stotype2name() const'
/usr/bin/ld: /home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:289: undefined reference to `vcg::ply::PropDescriptor::stotypename() const'
/usr/bin/ld: /home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:296: undefined reference to `vcg::ply::PropDescriptor::stotypename() const'
/usr/bin/ld: /home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:296: undefined reference to `vcg::ply::PropDescriptor::stotype2name() const'
/usr/bin/ld: CMakeFiles/vcg_evt.dir/src/main.cpp.o: in function `vcg::tri::io::ExporterPLY<PMesh>::Save(PMesh const&, char const*, bool, vcg::tri::io::PlyInfo const&, bool (*)(int, char const*))':
/home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:211: undefined reference to `vcg::ply::PropDescriptor::stotypename() const'
/usr/bin/ld: /home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:218: undefined reference to `vcg::ply::PropDescriptor::stotypename() const'
/usr/bin/ld: /home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:218: undefined reference to `vcg::ply::PropDescriptor::stotype2name() const'
/usr/bin/ld: /home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:289: undefined reference to `vcg::ply::PropDescriptor::stotypename() const'
/usr/bin/ld: /home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:296: undefined reference to `vcg::ply::PropDescriptor::stotypename() const'
/usr/bin/ld: /home/era/workspace/lib/vcglib/wrap/io_trimesh/export_ply.h:296: undefined reference to `vcg::ply::PropDescriptor::stotype2name() const'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/vcg_evt.dir/build.make:103: vcg_evt] Error 1
make[2]: *** [CMakeFiles/Makefile2:95: CMakeFiles/vcg_evt.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:102: CMakeFiles/vcg_evt.dir/rule] Error 2
make: *** [Makefile:137: vcg_evt] Error 2

解决办法

包含path-to-vcglib/wrap/ply/plylib.cpp 有三种方法,我也是刚接触Cmake,不确定这三种用法是否恰当,总之是实现了我的要求, 总结下就是带有source的指令都是和cpp源文件有关的,带include都是和.h头文件有关的。

  1. 使用aux_source_directory包含目录path-to-vcglib/wrap/ply/并在add_executable添加该变量
    # VCG_PLY是随便起得名字 ${SOURCES}是我自己的代码,/home/era/workspace/lib 是我放库文件的地方
    aux_source_directory(/home/era/workspace/lib/vcglib/wrap/ply VCG_PLY)
    add_executable(${PROJECT_NAME} ${SOURCES} ${VCG_PLY})
    
  2. 使用file找到该cpp文件,并在add_executable添加该变量
    # file GLOB 是查找下面一行匹配的文件赋值给GLOB 后面的变量
    file(
        GLOB VCG_PLY
            /home/era/workspace/lib/vcglib/wrap/ply/plylib.cpp
    )
     add_executable(${PROJECT_NAME} ${SOURCES} ${VCG_PLY})
    
  3. add_executable之后使用target_sources添加该文件
    add_executable(${PROJECT_NAME} ${SOURCES})
    target_sources(${PROJECT_NAME} PUBLIC /home/era/workspace/lib/vcglib/wrap/ply/plylib.cpp)
    

最后附上完整CMakeLists.txt

cmake_minimum_required(VERSION 3.13)
project(vcg_evt)

set(VCG_DIR /home/era/workspace/lib/vcglib)

aux_source_directory(src SOURCES)

include_directories(
        ${VCG_DIR}
        # 如果系统内没有安装Eigen
        ${VCG_DIR}/eigenlib
        include
)

# 需要包含 path-to-vcglib/wrap/ply/plylib.cpp
aux_source_directory(/home/era/workspace/lib/vcglib/wrap/ply VCG_PLY)

add_executable(${PROJECT_NAME} ${SOURCES} ${VCG_PLY})

set_target_properties(${PROJECT_NAME} PROPERTIES DCMAKE_GENERATOR_PLATFORM "x64")

参考

  1. https://www.codenong.com/cs106160295/