LiangliangNan / Easy3D

A lightweight, easy-to-use, and efficient C++ library for processing and rendering 3D data
GNU General Public License v3.0
1.35k stars 243 forks source link

Questions about the using in an external cmake project #188

Closed zhanghua7099 closed 2 months ago

zhanghua7099 commented 2 months ago

Hi! I have build Easy3D follow the method:

    $ cd path-to-root-dir-of-Easy3D
    $ mkdir Release
    $ cd Release
    $ cmake -DCMAKE_BUILD_TYPE=Release ..
    $ make
    $ sudo make install

I follow the method from https://github.com/LiangliangNan/Easy3D?tab=readme-ov-file#use-easy3d-in-your-project

and the cmake will output the following errors:

CMake Error at CMakeLists.txt:4 (find_package):
  By not providing "FindEasy3D.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Easy3D", but
  CMake did not find one.

  Could not find a package configuration file provided by "Easy3D" with any
  of the following names:

    Easy3DConfig.cmake
    easy3d-config.cmake

  Add the installation prefix of "Easy3D" to CMAKE_PREFIX_PATH or set
  "Easy3D_DIR" to a directory containing one of the above files.  If "Easy3D"
  provides a separate development package or SDK, be sure it has been
  installed.

How to get the file "FindEasy3D.cmake"? My system is Ubuntu 20.04.

LiangliangNan commented 2 months ago

I think the last paragraph of the error message has already given you the hint :-)

After you build Easy3D, you will find the file Easy3DConfig.cmake in the build (or installation) directory, e.g., /Users/lnan/Documents/Projects/Easy3D/cmake-build-release or (if you have installed Easy3d) /usr/local/Cellar/easy3d/2.5.2/lib/CMake/Easy3D/Easy3DConfig.cmake

After CMake (of course it shows the error message), open the CMake cache file CMakeCache.txt and you will see the following line: Easy3D_DIR:PATH=Easy3D_DIR-NOTFOUND You can simply replace Easy3D_DIR-NOTFOUND with any of the above paths. Rerun CMake and it should work now.

In case you use the UI version of CMake, you first run CMake and you will encounter the error. Then you can simply search Easy3D_DIR, pass one of the above paths to it, and rerun CMake.

BTW, this process is standard for almost all software based on the CMake build system (meaning your question is not specific to Easy3D, but the usage of CMake).

zhanghua7099 commented 2 months ago

Hi! Thanks for the tips about cmake!

I solved this problem using the following steps. I run the sudo make install thus the Easy3D installed at /usr/local/easy3d-2.5.3/.

The external project is organized as:

Easy3DTest
├── cmake
│   ├── Easy3DTargets.cmake
│   ├── Easy3DTargets-noconfig.cmake
│   └── FindEasy3D.cmake
├── CMakeLists.txt
└── main.cpp

The file in the folder cmake is copied from

/usr/local/easy3d-2.5.3/lib/CMake/Easy3D/Easy3DTargets.cmake
/usr/local/easy3d-2.5.3/lib/CMake/Easy3D/Easy3DTargets-noconfig.cmake
/usr/local/easy3d-2.5.3/lib/CMake/Easy3D/Easy3DConfig.cmake

Important!: The Easy3DConfig.cmake is renamed as FindEasy3D.cmake so that cmake can find Easy3D.

The CMakeLists.txt is modified to

cmake_minimum_required(VERSION 3.12)
project(MyProject)
set(CMAKE_CXX_STANDARD 11)                       # specify C++ standard
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(Easy3D COMPONENTS viewer REQUIRED)  # request Easy3D (recommended to request only needed components)
add_executable(Test main.cpp)                    # create an executable target
target_link_libraries(Test easy3d::viewer)       # link to necessary Easy3D modules (add more if needed, e.g., algo)

The demo can be built in my computer.

mkdir build && cd build && cmake .. && make
./Test

image

Thanks again for your great works!

LiangliangNan commented 2 months ago

Glad it work. But I still want to point out that your way of renaming the file is not standard and thus not recommended.