Building a static library that depends on wxWidgets

170 views
Skip to first unread message

damateem

unread,
Jun 15, 2012, 11:09:47 PM6/15/12
to wx-u...@googlegroups.com
I've built a static library that utilizes wxWidgets. It built without
errors, but I'm having linking issues when I link the library into my
application. When the application links, the linker reports that there
are multiple definitions of wxWidgets classes and functions.

What might I be doing wrong? Is there documentation somewhere that will
step me through this process?

Thanks,
David

wxWidgets 2.8.11
MS Windows
MinGW

Lluís Batlle i Rossell

unread,
Jun 16, 2012, 5:03:43 AM6/16/12
to wx-u...@googlegroups.com
On Fri, Jun 15, 2012 at 11:09:47PM -0400, damateem wrote:
> I've built a static library that utilizes wxWidgets. It built
> without errors, but I'm having linking issues when I link the
> library into my application. When the application links, the linker
> reports that there are multiple definitions of wxWidgets classes and
> functions.
>
> What might I be doing wrong? Is there documentation somewhere that
> will step me through this process?

I don't think there is any difference whether it is wxWidgets, or any other
library, in this your linking process.

The linker will tell you where are the multiple definitions.

Regards,
Lluís.

damateem

unread,
Jun 16, 2012, 9:31:57 AM6/16/12
to wx-u...@googlegroups.com
True, but how do I prevent the multiple definitions? Since my library
uses wxWidgets, MinGW is including it in my library.

It seems that I need a way to tell MinGW to exclude wxWidgets from my
library.

How do I do that?

Nathan Ridge

unread,
Jun 16, 2012, 4:00:48 PM6/16/12
to wxWidgets Mailing List

----------------------------------------
> Date: Sat, 16 Jun 2012 09:31:57 -0400
> From: dama...@gmail.com
> To: wx-u...@googlegroups.com
> Subject: Re: Building a static library that depends on wxWidgets
MinGW should not include wxWidgets in your library unless you tell it to.

What commands do you use to build your library?

Regards,
Nate

damateem

unread,
Jun 17, 2012, 5:30:28 PM6/17/12
to wx-u...@googlegroups.com

On 6/16/2012 4:00 PM, Nathan Ridge wrote:
> ----------------------------------------
>> Date: Sat, 16 Jun 2012 09:31:57 -0400
>> From: dama...@gmail.com
>> To: wx-u...@googlegroups.com
>> Subject: Re: Building a static library that depends on wxWidgets
>>
>> On 6/16/2012 5:03 AM, Llu�s Batlle i Rossell wrote:
>>> On Fri, Jun 15, 2012 at 11:09:47PM -0400, damateem wrote:
>>>> I've built a static library that utilizes wxWidgets. It built
>>>> without errors, but I'm having linking issues when I link the
>>>> library into my application. When the application links, the linker
>>>> reports that there are multiple definitions of wxWidgets classes and
>>>> functions.
>>>>
>>>> What might I be doing wrong? Is there documentation somewhere that
>>>> will step me through this process?
>>> I don't think there is any difference whether it is wxWidgets, or any other
>>> library, in this your linking process.
>>>
>>> The linker will tell you where are the multiple definitions.
>> True, but how do I prevent the multiple definitions? Since my library
>> uses wxWidgets, MinGW is including it in my library.
>>
>> It seems that I need a way to tell MinGW to exclude wxWidgets from my
>> library.
>>
>> How do I do that?
> MinGW should not include wxWidgets in your library unless you tell it to.
>
> What commands do you use to build your library?

Just to prove I could get this to work at the simplest level, I created
two very small example projects. One a static library that utilizes
wxWidgets and another, a wxWidgets application that utilizes the library.

The library contains only one function,

void Dummy( wxString &Param )
{
Param =_T("Dummy string");
}

which it called from a button click handler in the application.

void test_appFrame::OnbtnTestClick(wxCommandEvent& event)
{
wxString s;

Dummy( s );

wxMessageBox( s );
}

The library builds fine.

mingw32-g++.exe -Wall -g
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\include
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\contrib\include
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\lib\gcc_lib\mswud
-c C:\user_data\david\code\test_lib\test_lib.cpp -o obj\Debug\test_lib.o
ar.exe -r -s libtest_lib.a obj\Debug\test_lib.o
ar.exe: creating libtest_lib.a

However, the application does not.

mingw32-g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__
-DwxUSE_UNICODE -Wno-attributes -Winvalid-pch -include wx_pch.h
-DWX_PRECOMP -Wall -g -D__WXDEBUG__
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\include
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\contrib\include
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\lib\gcc_lib\mswud
-c C:\user_data\david\code\test_lib\wx_pch.h -o
wx_pch.h.gch\Debug_wx_pch_h_gch

mingw32-g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__
-DwxUSE_UNICODE -Wno-attributes -Winvalid-pch -include wx_pch.h
-DWX_PRECOMP -Wall -g -D__WXDEBUG__
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\include
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\contrib\include
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\lib\gcc_lib\mswud
-c C:\user_data\david\code\test_lib\test_appMain.cpp -o
obj\Debug\test_appMain.o

mingw32-g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__
-DwxUSE_UNICODE -Wno-attributes -Winvalid-pch -include wx_pch.h
-DWX_PRECOMP -Wall -g -D__WXDEBUG__
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\include
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\contrib\include
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\lib\gcc_lib\mswud
-c C:\user_data\david\code\test_lib\test_appApp.cpp -o
obj\Debug\test_appApp.o
windres.exe -i C:\USER_D~1\david\code\test_lib\resource.rc -J rc -o
obj\Debug\resource.res -O coff
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\include
-IC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\lib\gcc_lib\mswud

mingw32-g++.exe
-LC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\lib\gcc_lib -L.
-o bin\Debug\test_app.exe obj\Debug\test_appApp.o
obj\Debug\test_appMain.o obj\Debug\resource.res -mthreads -ltest_lib
-lwxmsw28ud -lwxpngd -lwxjpegd -lwxtiffd -lwxzlibd -lkernel32 -luser32
-lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32
-luuid -lcomctl32 -lwsock32 -lodbc32 -mwindows

./libtest_lib.a(test_lib.o):C:/user_data/david/dajac/source_pool/wxWidgets-2.8.11/include/wx/string.h:965:
undefined reference to `wxStringBase::operator=(char const*)'
collect2: ld returned 1 exit status

Process terminated with status 1 (0 minutes, 7 seconds)
1 errors, 0 warnings


I've tried moving "-ltest_lib" after "-lwxmsw28ud", but I get the same
error.

wxmsw28ud is being linked into the application, so why the undefined
reference error?

Thanks,
David

Nathan Ridge

unread,
Jun 18, 2012, 2:31:35 PM6/18/12
to wxWidgets Mailing List

> mingw32-g++.exe
> -LC:\user_data\david\dajac\source_pool\wxWidgets-2.8.11\lib\gcc_lib -L.
> -o bin\Debug\test_app.exe obj\Debug\test_appApp.o
> obj\Debug\test_appMain.o obj\Debug\resource.res -mthreads -ltest_lib
> -lwxmsw28ud -lwxpngd -lwxjpegd -lwxtiffd -lwxzlibd -lkernel32 -luser32
> -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32
> -luuid -lcomctl32 -lwsock32 -lodbc32 -mwindows
>
> ./libtest_lib.a(test_lib.o):C:/user_data/david/dajac/source_pool/wxWidgets-2.8.11/include/wx/string.h:965:
> undefined reference to `wxStringBase::operator=(char const*)'
> collect2: ld returned 1 exit status
>
> [snip]
>
> wxmsw28ud is being linked into the application, so why the undefined
> reference error?

There are other wxWidgets static libraries that you need to link to besides
wxmsw28ud, wxpngd, wxjpegd, wxtiffd, and wxzlibd. In particular, wxString
members should be in a library called "wxbase28ud" or similar.

Check your wxWidgets-2.8.11\lib\gcc_lib folder and make sure you link to *all*
the static libraries in there.

Regards,
Nate

Kobus Grobler

unread,
Jun 18, 2012, 4:28:58 PM6/18/12
to wx-u...@googlegroups.com
You compile the library with different options than the app.
In this case you use -DwxUSE_UNICODE when compiling the app, but not the
test_lib library which is in ascii/utf mode depending on setup.h.

Best
Kobus

damateem

unread,
Jun 23, 2012, 5:48:16 PM6/23/12
to wx-u...@googlegroups.com
I use a monolithic build so only one library is needed.

Thanks,
David

damateem

unread,
Jun 23, 2012, 5:49:12 PM6/23/12
to wx-u...@googlegroups.com
That did the trick! Building the library with the unicode option makes
it all work.

Thank You
David

Reply all
Reply to author
Forward
0 new messages