Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use external DLL in cmake build

I'm working on the cmake scripts for my project and I've run into a problem:

My project uses a 3rd party library (FreeImage), which has its own Makefile-based build system. I can build FreeImage just fine by simply running "make" (I'm using gnuwin32), which will build FreeImage using MinGW and produce:

FreeImage.lib
FreeImage.dll

Now my problem is twofold:

  1. I want to execute "make" from my cmake script.
  2. I want to link to the import lib (FreeImage.lib), and also make sure the DLL gets copied to the correct place so the EXE will run.

I know how to link to the LIB file, but I'm lost on the rest.

The folder structure is like this:

MyProject                     # main directory
MyProject/Libs/FreeImage      # FreeImage root directory
MyProject/Libs/FreeImage/Dist # This is where FreeImage outputs go (LIB and DLL)

BTW: I'm running on Windows 7. I plan to build my project both with MSVC and MinGW.

Thanks!

EDIT: I'm now trying to use ExternalProject_Add like so:

ExternalProject_Add(
    FreeImage
    PREFIX ./Libs/FreeImage
    URL ./Libs/FreeImage
    BUILD_COMMAND make
)

This gets me part of the way there, but doesn't totally work... it tries to configure things for me and tries to use nmake... ugh

like image 493
sidewinderguy Avatar asked Apr 17 '11 22:04

sidewinderguy


1 Answers

In my opinion, there are two options:

In case you have put your FreeImage sources in your projects' source-tree, the easiest option may be to use the execute_process() command. Assuming FreeImage is in your projects' source-tree in "3rdparty/FreeImage/" you can do something like,

execute_process( COMMAND make WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/3rdParty/FreeImage )

Optionally, you can copy the dll from 3rdParty/FreeImage/bin into you own bin directory. And then you can write a FreeImageConfig.cmake for importing the library:

add_library( FreeImage IMPORTED ) set_target_properties( FreeImage PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/3rdParty/FreeImage/lib ) ...

The other option is to make use of the ExternalProject module. You can also take a look at this article from Kitware for an overview of this module. In essence, you specify the full chain of commands needed to get the source, configure the build, build the source and install it. All in your own CMakeLists.txt

like image 193
André Avatar answered Oct 07 '22 07:10

André