I have a library I need to integrate into my project, I can use file(DOWNLOAD …) to grab the tar ball and I can use file(ARCHIVE_EXTRACT …) to extract the sources, the problem I have is that the tarball has a root folder which is versioned and that is causing me problems when it comes to using the library.

I’ve searched and tried loads of things and thought PATTERNS might help, but I couldn’t solve it.

The archive is like this:


I have a CMakeLists that resides in ThirdPartyLib as the library doesn’t support CMake, that I want to do is extract the archive and ignore the top level directory so I end up with:


I tried file(RENAME …) but that runs at the point when CMake is invoked and not when the build is performed, so it errors telling me that the folder doesn’t exist when I invoke cmake.

Any thoughts on how to do this?

I tried ExternalProject_Add, but that doesn’t work either as I have to invoke cmake for the configure stage and it doesn’t inherit definitions from the parent CMakeFiles.txt.


Take a look at FetchContent, I think it provides the functionality you are looking for.

1 Like

This looks like it might be what I’m after! Thank you.

Well, it nearly worked. I have an issue of how to set a variable because it doesn’t seem to inherit and I can’t find a way of setting the variable on the FetchContents modules. My application is composed of libraries and plugins and the binaries have to go to a specific location.

I don’t understand what you are trying to say here?

Let me try and be clearer, you might end up more confused though! lol

My application consists of a load of shared libraries and plugins. The main application is nothing more than a plugin loader, it loads the plugins in the right order so that dependencies are recolsved.

So I have a folder structure that is like

stc/lin/shared lib 1

stc/lin/shared lib n
src/compoentts/shared library 4

src/components/shaed library 5

The components are a shared library with a metadata stub inside them which provides the dependencies

The lib folder consists of a load of libraries which are both mine and third party.

There’s cmake include file which contains a lot of macros to hide away the complexity, it is basically a meta language.

Now I have this library I want to use, it does not have a CMakeLists.txt, so I have been trying to find a way to populate arc/the lib/libinquestion, following the suggestion above I used the FetchContent module to download this libraries tar ball and copy over my CMakeLists.txt.

The first issue I had was that the tarball is in has a root folder of coollibrary-1.2.3, this is no good for me, I need it to be in the folder.

Now the suggestion above, worked, I was able to exact the library archive to coollibrary and inside that is a src folder, so I now can just tell my CMakeLists.txt file for this library which source files need to be compiled. That again works with the solution presented above.

The issue I have now, is that as the CMakeLists.txt nest, they expose certain variables to each level of the script, these are important for me because of the architecture of the application, it builds to a bin/x86_64 and creates subdirectories any places the libraries and components in the correct place so that the application will run,

what I can’t seem to do is pass variables that have been defined up the build tree into this module. Lets say the CMakeLists.txt in src/lib addds the subdirectories by adds a variable called SHARED_LIB_FOLDER which code further down the tree can use.

This works perfectly in the general sense, but as soon as I use the FetchContent none of the variables are inherited and now I don’t know where to put files.

What I need is a way to adding libraries to my src/libs in such a way that variables get passed to it.