Problems using wxWidgets in a shared library

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
drjay
In need of some credit
In need of some credit
Posts: 3
Joined: Tue Feb 02, 2021 8:45 pm

Problems using wxWidgets in a shared library

Post by drjay »

Hello,
I am developing a cross-platform C++ wxWidgets application that runs on Windows x64 and 64-bit linux. I'm using vs2019 on Windows and gcc 9.3.0 on xubuntu 20.04. I'm using wxWidgets 3.1.4 on both platforms.

My application links to 3 support libraries that I've developed that use wx objects throughout. On windows, I use vcpkg and can build both static and dynamic versions of the libraries that link with my application and run with no problem.

On linux, I use the wx binaries supplied with the xubuntu distro, which are shared libraries only for 64-bit builds. I've found that I can only build my libraries as shared in order to get them to successfully link into my application; I cannot build a static library that links with the shared wxWidgets libraries. Nevertheless, when I run the application linked with my shared libraries on linux, I get a segmentation fault when the first graphics object is initialized (its a wxColour object). Below is the line of code and the message I get:

old_transcol = wxColour( trans_col );

Thread 1 "task1" received signal SIGSEGV, Segmentation fault.
0x00007ffff74dcc45 in wxRefCounter::DecRef (this=0x7fff) at ../src/common/object.cpp:339

As a workaround, I hoisted all the source code from each of my libraries directly into the application source and built it successfully as one monolithic app. After doing so, the application runs flawlessly on linux with no major problems, and there is no seg fault when the first graphics object is called.

I'm stumped. The old_transcol is a wxColour object declared in the object body and initialized by the statement above. I'm wondering if there is some initialization required in my libraries, as the "old_transcol" would likely be created when the object is constructed, which happens before the main() function is called.

Below is the output from wx-config --cxxflags --libs when run on my linux machine:

-I/usr/lib/x86_64-linux-gnu/wx/include/gtk3-unicode-3.1-unofficial3 -I/usr/include/wx-3.1-unofficial -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread
-L/usr/lib/x86_64-linux-gnu -pthread -lwx_gtk3u_unofficial3_xrc-3.1 -lwx_gtk3u_unofficial3_html-3.1 -lwx_gtk3u_unofficial3_qa-3.1 -lwx_gtk3u_unofficial3_core-3.1 -lwx_baseu_unofficial3_xml-3.1 -lwx_baseu_unofficial3_net-3.1 -lwx_baseu_unofficial3-3.1

I've verified the same wx-config flags are being used when compiling each source file.
I've also verified that all the wx libraries are specified on the linker command line prior to my libraries.

Any help sorting this out is appreciated! Thanks!
User avatar
doublemax
Moderator
Moderator
Posts: 19102
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problems using wxWidgets in a shared library

Post by doublemax »

My application links to 3 support libraries that I've developed that use wx objects throughout. On windows, I use vcpkg and can build both static and dynamic versions of the libraries that link with my application and run with no problem.
If the support libraries also use wxWidgets, you must link wxWidgets dynamically everywhere. Otherwise you'll end up with multiple wx instances that don't know about each other.

The old_transcol is a wxColour object declared in the object body and initialized by the statement above.
What do you mean with "object body"? It must not be a global variable.
Use the source, Luke!
drjay
In need of some credit
In need of some credit
Posts: 3
Joined: Tue Feb 02, 2021 8:45 pm

Re: Problems using wxWidgets in a shared library

Post by drjay »

The transcol is declared as a member of an object. My bad.
drjay
In need of some credit
In need of some credit
Posts: 3
Joined: Tue Feb 02, 2021 8:45 pm

Re: Problems using wxWidgets in a shared library

Post by drjay »

I think it will be ok to use the shared libraries everywhere. I build the my shared libraries but as far as I know the linker is not linking to them at that time, its simply creating my .so libraries that should link up when I build the main application. I may be wrong.
Post Reply