Link to a library built with mingw on Cygwin - c

I have built libmysqlclient using mingw on Cygwin, so I get the ".a" file, but when I try to link with this file, it fails.
My compiler is mingw, on GNU/Linux and I target Windows.
Here is the command line to link the program (generated by automake) :
i586-mingw32msvc-g++ -s -O1 -Wall -Wclobbered -Wempty-body -Wignored-qualifiers -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -mwindows -o .libs/valoisscan.exe App.o ConfigManager.o DialogConfig.o DialogRCDStart.o DialogAbout.o findFile.o MainFrame.o PanelScan.o PanelSearch.o MyException.o strWxStdConv.o libsqlendive.o PanelBase.o rc.o -mthreads -Wl,--subsystem -Wl,windows -mwindows -L/usr/i586-mingw32msvc//lib /usr/i586-mingw32msvc//lib/libwx_mswu_richtext-2.8.a /usr/i586-mingw32msvc//lib/libwx_mswu_aui-2.8.a /usr/i586-mingw32msvc//lib/libwx_mswu_xrc-2.8.a /usr/i586-mingw32msvc//lib/libwx_mswu_qa-2.8.a /usr/i586-mingw32msvc//lib/libwx_mswu_html-2.8.a /usr/i586-mingw32msvc//lib/libwx_mswu_adv-2.8.a /usr/i586-mingw32msvc//lib/libwx_mswu_core-2.8.a /usr/i586-mingw32msvc//lib/libwx_baseu_xml-2.8.a /usr/i586-mingw32msvc//lib/libwx_baseu_net-2.8.a /usr/i586-mingw32msvc//lib/libwx_baseu-2.8.a -lwxregexu-2.8 -lwxexpat-2.8 -lwxtiff-2.8 -lwxjpeg-2.8 -lwxpng-2.8 -lwxzlib-2.8 -lrpcrt4 -loleaut32 -lole32 -luuid -lwinspool -lwinmm -lshell32 -lcomctl32 -lcomdlg32 -lctl3d32 -ladvapi32 -lwsock32 -lgdi32 ticpp/.libs/libticpp.a mysqlwrapped/.libs/libmysqlwrapped.a -lmysqlclient -lstdc++ -mthreads
Here are the linker errors :
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x12b): undefined reference to `_mysql_ping#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x17b): undefined reference to `_mysql_errno#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x1d5): undefined reference to `_mysql_error#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x2f0): undefined reference to `_mysql_num_rows#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x31e): undefined reference to `_mysql_insert_id#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x349): undefined reference to `_mysql_fetch_row#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x3bd): undefined reference to `_mysql_query#8'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x5da): undefined reference to `_mysql_free_result#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0xb8c): undefined reference to `_mysql_free_result#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0xd9c): undefined reference to `_mysql_free_result#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x1712): undefined reference to `_mysql_store_result#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x172b): undefined reference to `_mysql_fetch_field#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Query.o):Query.cpp:(.text+0x1962): undefined reference to `_mysql_fetch_field#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Database.o):Database.cpp:(.text+0x3c2): undefined reference to `_mysql_options#12'
mysqlwrapped/.libs/libmysqlwrapped.a(Database.o):Database.cpp:(.text+0x129c): undefined reference to `_mysql_init#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Database.o):Database.cpp:(.text+0x135d): undefined reference to `_mysql_real_connect#32'
mysqlwrapped/.libs/libmysqlwrapped.a(Database.o):Database.cpp:(.text+0x1405): undefined reference to `_mysql_real_connect#32'
mysqlwrapped/.libs/libmysqlwrapped.a(Database.o):Database.cpp:(.text+0x14c0): undefined reference to `_mysql_ping#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Database.o):Database.cpp:(.text+0x155a): undefined reference to `_mysql_ping#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Database.o):Database.cpp:(.text+0x15eb): undefined reference to `_mysql_close#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Database.o):Database.cpp:(.text+0x1ad9): undefined reference to `_mysql_close#4'
mysqlwrapped/.libs/libmysqlwrapped.a(Database.o):Database.cpp:(.text+0x1e1b): undefined reference to `_mysql_close#4'
I tried strings libmysqlclient.a | grep _mysql_ping and it returned two results, so the lib looks valid.
Command i586-mingw32msvc-objdump -x libmysqlclient.a | grep mysql_ping outputs [ 39](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x000012f0 _mysql_ping
It might be important : my target for gcc is i586-mingw32msvc

Since you are using C++ you might need to wrap your inclusion of mysqlclient.h in an extern "C":
extern "C" {
#include <mysqlclient.h>
}
C++ name mangling might be renaming the symbols so they can't be found by the linker.

You might need to use -Wl,--enable-stdcall-fixup during linking to not link against the decorated stdcall functions. Most libraries tend to export undecorated symbols even though they are stdcall.
Other flags to try are -Wl,--kill-at and -Wl,--add-stdcall-alias.

Related

libpdcurses.a setting for Pdcurses

I'm working on a program in which I use PDCurses3.5 functions using i686-w64-mingw32-gcc.exe.
When I compile the program, I keep getting errors such as "undefined reference to 'COLS'", "undefined reference to 'lines'".
I have checked that <curses.h> header and the library package properly installed.
Here is my input line:
> i686-w64-mingw32-gcc.exe set.o read.o elements.o random.o
> -L../standard/test -lplotfit -lplotget -lgfortran -Wl,--subsystem,console -mwindows -o runtime/mingw/result -lm -static -lws2_32 -lpdcurses
And the first part of the error is:
../standard/bin/mingw/menu.o:menu.c:(.text+0xb): undefined reference to `COLS'
../standard/bin/mingw/menu.o:menu.c:(.text+0x16): undefined reference to `COLS'
../standard/bin/mingw/menu.o:menu.c:(.text+0x33): undefined reference to `LINES'
../standard/bin/mingw/menu.o:menu.c:(.text+0x47): undefined reference to `MOVE'
../standard/bin/mingw/menu.o:menu.c:(.text+0x74): undefined reference to `initscr'
...
It seems the program cannot refer to libpdcurses.a in its library file.
What am I doing wrong?
When you use -lpdcurses, the linker looks for libpdcurses.a in certain predefined locations, plus those specified via -L. But, by default, the library is built as pdcurses.a. To link it, you can directly specify its location; e.g.:
gcc -oprogname.exe progname.c pdcurses.a
or
gcc -oprog2.exe prog2.c /pdcurses/win32/pdcurses.a
Alternatively, you can rename the library to libpdcurses.a, and either copy it to a location in the existing search path, or use -L:
gcc -oprogname.exe progname.c -lpdcurses
or
gcc -oprog2.exe prog2.c -L/pdcurses/win32 -lpdcurses

mingw+libarchive: cross compiling linker error

I'm trying to cross compile a tiny program under Linux for win32. It uses libarchive, which is available in for mingw on my fedora 23 installation (all installed through dnf), but I get lots of linker errors.
Source: https://github.com/panzi/save_the_zazus
Compiler/linker output:
panzi#panzi save_the_zazus [master*]$ mkdir -p package/img
panzi#panzi save_the_zazus [master*]$ touch package/img/atlas0.png package/img/atlas1.png package/img/atlas2.png
panzi#panzi save_the_zazus [master*]$ make TARGET=win32 setup
mkdir -p build/win32 build/src
panzi#panzi save_the_zazus [master*]$ make TARGET=win32
i686-w64-mingw32-gcc -m32 -DLIBARCHIVE_STATIC -Wall -Werror -Wextra -std=gnu11 -Ibuild/src -O2 -c src/save_the_zazus.c -o build/win32/save_the_zazus.o
xxd -i package/img/atlas0.png > build/src/package_img_atlas0_png.c
i686-w64-mingw32-gcc -m32 -DLIBARCHIVE_STATIC -Wall -Werror -Wextra -std=gnu11 -Ibuild/src -O2 -c build/src/package_img_atlas0_png.c -o build/win32/package_img_atlas0_png.o
xxd -i package/img/atlas1.png > build/src/package_img_atlas1_png.c
i686-w64-mingw32-gcc -m32 -DLIBARCHIVE_STATIC -Wall -Werror -Wextra -std=gnu11 -Ibuild/src -O2 -c build/src/package_img_atlas1_png.c -o build/win32/package_img_atlas1_png.o
xxd -i package/img/atlas2.png > build/src/package_img_atlas2_png.c
i686-w64-mingw32-gcc -m32 -DLIBARCHIVE_STATIC -Wall -Werror -Wextra -std=gnu11 -Ibuild/src -O2 -c build/src/package_img_atlas2_png.c -o build/win32/package_img_atlas2_png.o
i686-w64-mingw32-gcc -m32 -DLIBARCHIVE_STATIC -static -larchive build/win32/save_the_zazus.o build/win32/package_img_atlas0_png.o build/win32/package_img_atlas1_png.o build/win32/package_img_atlas2_png.o -o build/win32/save_the_zazus.exe
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x301): undefined reference to `archive_read_new'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x31d): undefined reference to `archive_read_support_filter_all'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x3cd): undefined reference to `archive_entry_free'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x3df): undefined reference to `archive_read_free'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x3ef): undefined reference to `archive_write_free'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x426): undefined reference to `archive_error_string'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x462): undefined reference to `archive_error_string'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x4a1): undefined reference to `archive_read_support_format_all'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x538): undefined reference to `archive_read_open_filename'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x545): undefined reference to `archive_write_new'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x557): undefined reference to `archive_write_set_format_zip'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x56b): undefined reference to `archive_write_set_options'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x581): undefined reference to `archive_error_string'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x5c9): undefined reference to `archive_write_open_filename'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x5d9): undefined reference to `archive_error_string'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x61e): undefined reference to `archive_error_string'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x67a): undefined reference to `archive_entry_new2'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x6ba): undefined reference to `archive_read_next_header2'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x6d0): undefined reference to `archive_entry_pathname'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x727): undefined reference to `archive_entry_new2'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x73d): undefined reference to `archive_entry_copy_pathname'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x74b): undefined reference to `archive_entry_perm'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x75a): undefined reference to `archive_entry_set_perm'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x762): undefined reference to `archive_entry_filetype'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x771): undefined reference to `archive_entry_set_filetype'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x77f): undefined reference to `archive_entry_mode'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x78e): undefined reference to `archive_entry_set_mode'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x7a5): undefined reference to `archive_entry_set_size'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x7b3): undefined reference to `archive_entry_uid'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x7c3): undefined reference to `archive_entry_set_uid'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x7d1): undefined reference to `archive_entry_gid'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x7e1): undefined reference to `archive_entry_set_gid'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x7ef): undefined reference to `archive_entry_mtime_nsec'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x803): undefined reference to `archive_entry_mtime'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x819): undefined reference to `archive_entry_set_mtime'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x82b): undefined reference to `archive_write_header'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x83b): undefined reference to `archive_entry_free'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x854): undefined reference to `archive_write_data'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x870): undefined reference to `archive_error_string'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x904): undefined reference to `archive_write_header'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x91a): undefined reference to `archive_entry_size'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x948): undefined reference to `archive_read_data'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x96c): undefined reference to `archive_write_data'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x988): undefined reference to `archive_error_string'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0x9dd): undefined reference to `archive_write_add_filter_none'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0xa13): undefined reference to `archive_error_string'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0xa47): undefined reference to `archive_entry_free'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0xa8e): undefined reference to `archive_error_string'
build/win32/save_the_zazus.o:save_the_zazus.c:(.text.startup+0xad6): undefined reference to `archive_error_string'
collect2: error: ld returned 1 exit status
Makefile:82: recipe for target 'build/win32/save_the_zazus.exe' failed
make: *** [build/win32/save_the_zazus.exe] Error 1
For this single source file program I just wrote a simple Makefile. The png files aren't in the repo (yet), so just create empty files for them if you try to compile. I tried dynamic and static linking (would prefer static so all I need to do is to pass on a .exe file). For this output I used -static -larchive and it doesn't complain about any missing libraries, but I get all those undefined references.
Add -static -larchive to the end of your command line and try again. The linker isn't getting the library to link against. I've found it needs to be last on the command line.

Error while compiling PortAudio examples

(I am on Ubuntu) I am trying to run the PortAudio examples, but getting many errors (mentioned below). I have placed the header file portaudio.h in the directory of the program. I have no idea about it. I think it is linker error. Please help!
/tmp/cc5EbTlT.o: In function main':
paex_record.c:(.text+0x37e): undefined reference toPa_Initialize'
paex_record.c:(.text+0x397): undefined reference to Pa_GetDefaultInputDevice'
paex_record.c:(.text+0x3de): undefined reference toPa_GetDeviceInfo'
paex_record.c:(.text+0x436): undefined reference to Pa_OpenStream'
paex_record.c:(.text+0x45a): undefined reference toPa_StartStream'
paex_record.c:(.text+0x493): undefined reference to Pa_Sleep'
paex_record.c:(.text+0x4c2): undefined reference toPa_IsStreamActive'
paex_record.c:(.text+0x4eb): undefined reference to Pa_CloseStream'
paex_record.c:(.text+0x5fa): undefined reference toPa_GetDefaultOutputDevice'
paex_record.c:(.text+0x641): undefined reference to Pa_GetDeviceInfo'
paex_record.c:(.text+0x6b2): undefined reference toPa_OpenStream'
paex_record.c:(.text+0x6e3): undefined reference to Pa_StartStream'
paex_record.c:(.text+0x71c): undefined reference toPa_Sleep'
paex_record.c:(.text+0x728): undefined reference to Pa_IsStreamActive'
paex_record.c:(.text+0x74e): undefined reference toPa_CloseStream'
paex_record.c:(.text+0x77d): undefined reference to Pa_Terminate'
paex_record.c:(.text+0x7e5): undefined reference toPa_GetErrorText'
collect2: error: ld returned 1 exit status
Assuming you are compiling using gcc and you have a single C file foo.c, the compiler command would be
gcc -o foo foo.c -lrt -lasound -ljack -lpthread -lportaudio
The -l parameters are there to link the required libraries to your program, e.g. -lrt will link librt.a. The order does matter.
I got the required libraries from here: http://www.portaudio.com/docs/v19-doxydocs/compile_linux.html#comp_linux3. Don't know if they are correct. At least you need -lportaudio, obviously.
If the libraries are not found, you have to provide gcc a path, e.g.
gcc -L/usr/lib -o foo foo.c -lrt -lasound -ljack -lpthread -lportaudio
Regarding the header, you don't actually need to copy it into your program's directory. You'd rather include it as
#include <portaudio.h>
and add its directory to the include search path:
gcc -I/usr/include -L/usr/lib -o foo foo.c -lrt -lasound -ljack -lpthread -lportaudio
Of course, all this is better done in a Makefile.

Errors Creating A Shared Library DLL for SWIG Simple Lua Example (Windows 7)

I use MinGW to create, and my lua version is 5.1.4 the followings are my steps:
swig -lua example.i
gcc -c example_wrap.c -I C:\Lua\5.1\include
gcc -c example.c -I C:\Lua\5.1\include
gcc -shared example_wrap.o example.o -o example.dll
errors ocurrs at last step, here are some part of the errors information
example_wrap.o:example_wrap.c:(.text+0x2aef): undefined reference to `lua_gettop'
example_wrap.o:example_wrap.c:(.text+0x2afe): undefined reference to `lua_gettop'
example_wrap.o:example_wrap.c:(.text+0x2b2d): undefined reference to `lua_pushfstring'
example_wrap.o:example_wrap.c:(.text+0x2b38): undefined reference to `lua_error'
example_wrap.o:example_wrap.c:(.text+0x2b67): undefined reference to `lua_pushnumber'
example_wrap.o:example_wrap.c:(.text+0x2ec0): undefined reference to `lua_pushvalue'
example_wrap.o:example_wrap.c:(.text+0x2ee3): undefined reference to `lua_pushstring'
example_wrap.o:example_wrap.c:(.text+0x2efe): undefined reference to `lua_pushcclosure'
example_wrap.o:example_wrap.c:(.text+0x2f11): undefined reference to `lua_rawset'
example_wrap.o:example_wrap.c:(.text+0x2f24): undefined reference to `lua_pushstring'
example_wrap.o:example_wrap.c:(.text+0x2f3f): undefined reference to `lua_pushcclosure'
example_wrap.o:example_wrap.c:(.text+0x2f52): undefined reference to `lua_rawset'
collect2: ld returned 1 exit status
it seems that I didn't include the lua header?
so I also try these commands but no use
gcc -shared example_wrap.o example.o -I C:\Lua\5.1\include -o example.dll
so any suggestions?
Try to link with this command:
gcc -LC:\Lua\5.1\bin -shared example_wrap.o example.o -llua51 -o example.dll
the path in the -LC:\Lua\5.1\bin part should point to the directory where you have your lua51.dll (I assumed bin, as in my system, but change it to suit your installation).

undefined reference to curl_global_init, curl_easy_init and other function(C)

I am trying to use Curl in C.
I visited Curl official page, and copied sample source code.
below is the link:
http://curl.haxx.se/libcurl/c/sepheaders.html
when I run this code with command "gcc test.c",
the console shows message like below.
/tmp/cc1vsivQ.o: In function `main':
test.c:(.text+0xe1): undefined reference to `curl_global_init'
test.c:(.text+0xe6): undefined reference to `curl_easy_init'
test.c:(.text+0x10c): undefined reference to `curl_easy_setopt'
test.c:(.text+0x12e): undefined reference to `curl_easy_setopt'
test.c:(.text+0x150): undefined reference to `curl_easy_setopt'
test.c:(.text+0x17e): undefined reference to `curl_easy_cleanup'
test.c:(.text+0x1b3): undefined reference to `curl_easy_cleanup'
test.c:(.text+0x1db): undefined reference to `curl_easy_setopt'
test.c:(.text+0x1e7): undefined reference to `curl_easy_perform'
test.c:(.text+0x1ff): undefined reference to `curl_easy_cleanup'
I do not know how to solve this.
You don't link with the library.
When using an external library you must link with it:
$ gcc test.c -lcurl
The last option tells GCC to link (-l) with the library curl.
In addition to Joachim Pileborg's answer, it is useful to remember that gcc/g++ linking is sensitive to order and that your linked libraries must follow the things that depend upon them.
$ gcc -lcurl test.c
will fail, missing the same symbols as before. I mention this because I came to this page for forgetting this fact.
I have the same problem, but i use g++ with a make file.
This is a linker issue.
You need to add option -lcurl on the compiler and on the linker.
In my case on the make file:
CC ?= gcc
CXX ?= g++
CXXFLAGS += -I ../src/ -I ./ -DLINUX -lcurl <- compile option
LDFLAGS += -lrt -lpthread -lcurl <- linker option
Gerard
Depending how bad things are you might need an -L/somewhere in LDFLAGS to let the linker know where the libraries are. ldconfig is supposed to pick them up and find them on every boot but on a new machine it can take a little prodding, like adding a directory to your /etc/ld.so.conf.

Resources