简体   繁体   中英

How to use export custom target with CMake?

I'm trying to export all of the dependent targets of an engine I've been developing with export command. Everything works fine with dependencies, but when I call the command with main "Nabla" target, I get a following error:

 CMake Error in CMakeLists.txt:
 export called with target "Nabla" which requires target "openssl_build"
 that is not in any export set.

The problem is that the openssl_build is a custom target and I have no clue how to avoid this error, because when I try to export the target I get another error telling me that

-- Using static CRT ON
CMake Error at 3rdparty/CMakeLists.txt:556 (export):
  export given custom target "openssl_build" which may not be exported.

the following commit contains my changes to the engine in reference to export command

The custom target generating the error is here

I wonder if I could set a cmake property for the openssl_build to make it work, but I have been looking for useful properties in cmake docs and could not find anything

Thank you in advance!

This usually happens when you use add_subdirectory to consume library.

When you add add_subdirectory , CMake will consider, for example, the whole openssl project become part of your project. They are not separable if they are the same project.

If you build openssl as part of your project, it's very doubting that your project will run properly without also having openssl installed, let alone users consuming your package!

You could simply add the openssl libraries you depend on to the export set:

install(TARGETS openssl_build EXPORT NablaTargets)

But that's not the proper way.


The proper way would be to use a package manager, such as vcpkg to install the dependencies for you.

First, replace the add_subdirectory by a find_package and a link:

find_package(OpenSSL REQUIRED)
target_link_libraries(Nabla PUBLIC OpenSSL::SSL OpenSSL::Crypto)

Create vcpkg.json with this content:

{
    "name": "nabla",
    "version-string": "0.1",
    "dependencies": [
         "openssl"
    ]
}

And when you use cmake, add the argument -DCMAKE_TOOLCHAIN_FILE=/opt/vcpkg/scripts/buildsystems/vcpkg.cmake (or where you installed it) and let the package manager do the work for you.

Remember that when you use a new toolchain file, you must re-create a new build directory since it cannot be added after creating it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM