Home > other >  How do I link to a static library (libtiff) using CMake in my wxWidgets project?
How do I link to a static library (libtiff) using CMake in my wxWidgets project?

Time:02-12

For my wxWidgets project, I am trying to make the switch from my self-written Makefile to Cmake. I develop on macOS.

When I was writing that Makefile I ran into an issue with libtiff. I wanted to statically link my application so that I don't have to distribute any dylibs myself or rely on my users to install them. I built wxWidgets as a static library but when I compiled my code and checked my binary with otool I always found that my binary required a local dylib.

/usr/local/opt/libtiff/lib/libtiff.5.dylib

Finally I found a solution on here. In essence, in the linking line of my Makefile I replaced wx-config –-libs with LDFLAGS. LDFLAGS looks like this:

WXCONFIGLIBS := $(shell wx-config --libs)
WXCONFIGLIBS := $(WXCONFIGLIBS:-ltiff=/usr/local/opt/libtiff//lib/libtiff.a)
# I am not sure whether the double slash is a typo but it works so I don't change it
LDFLAGS := $(WXCONFIGLIBS)

Basically, I search-and-replaced -ltiff with the path to my static libtiff library.

Now I've managed to compile my project using Cmake. However, I'm getting the same warning message as I did when I battled my original issue.

ld: warning: dylib (/usr/local/lib/libtiff.dylib) was built for newer macOS version (11.0) than being linked (10.11)

How do I fix this? My CMakeLists contains these sections pertaining to wxWidgets:

find_package(wxWidgets REQUIRED gl core base OPTIONAL_COMPONENTS net)
include(${wxWidgets_USE_FILE})
...
add_executable(myapp ${SOURCES})
target_link_libraries(myapp ${wxWidgets_LIBRARIES})
set_property(TARGET myapp PROPERTY CXX_STANDARD 17)

I already tried running some search-and-replace shenanigans like

string(REPLACE "-ltiff" "/usr/local/opt/libtiff/lib/libtiff.a" wxWidgets_LIBRARIES ${wxWidgets_LIBRARIES})

But that doesn't work. It does replace -ltiff but also seems to remove the semicolons and whitespaces separating the different libraries.

I've been scouring the web for any clues as to what to do, but I don't seem to have a good enough grasp of libraries to fix this.

Any help would be greatly appreciated.

CodePudding user response:

Set wxWidgets_USE_STATIC=ON before calling find_package(wxWidgets). See the documentation for wxWidgets here: https://cmake.org/cmake/help/latest/module/FindwxWidgets.html

option(wxWidgets_USE_STATIC "Link to wxWidgets statically" ON)

find_package(wxWidgets REQUIRED gl core base OPTIONAL_COMPONENTS net)
include(${wxWidgets_USE_FILE})
...
add_executable(myapp ${SOURCES})
target_link_libraries(myapp PRIVATE ${wxWidgets_LIBRARIES})
target_compile_features(myapp PRIVATE cxx_std_17)

CodePudding user response:

My search-and-replace idea turned out to be not so bad. I was able to achieve the same outcome with Cmake as with my Makefile.

My problem was not using double quotes in the appropriate place. So instead of this:

string(REPLACE "-ltiff" "/usr/local/opt/libtiff/lib/libtiff.a" wxWidgets_LIBRARIES ${wxWidgets_LIBRARIES})

I simply needed to write:

string(REPLACE "-ltiff" "/usr/local/opt/libtiff/lib/libtiff.a" wxWidgets_LIBRARIES "${wxWidgets_LIBRARIES}")

So to solve my actual problem, I am calling this string() command just before the target_link_libraries() command.

  • Related