Author Topic: Error when linking custom libraries  (Read 7510 times)

Offline Vinther

  • Single posting newcomer
  • *
  • Posts: 4
Error when linking custom libraries
« on: June 22, 2008, 02:13:32 pm »
Hi!

I have four different projects, all in one Workspace. Three of them are libraries that I've written, and the fourth is a console application using all three libs. All four projects use the Allegro library, and so include "allegro.h" and have the .a file added in Build options->Linker settings->Link libraries.

Now, when I try to build my project, I can see that the libs are first compiled and linked without error, but when it comes to linking the application I get "undefined reference" errors about every allegro function in my libs.

[EDIT] How can it be that the library sources are compiled fine, only to be causing errors when they are linked to another project?

All and any help is greatly appreciated, and let me know if any more information is needed to solve the problem. Thank you!
« Last Edit: June 22, 2008, 02:48:08 pm by Vinther »

Offline libfab

  • Multiple posting newcomer
  • *
  • Posts: 45
Re: Error when linking custom libraries
« Reply #1 on: June 22, 2008, 03:17:54 pm »
To link a lib securely with CB you need to do three things:

1. define a dependency in you workspace file, by right-clicking on your main project's Projetc options > project dependencies
2. define link libraries in Build options>Linker settings tab

steps 1. and 2. correspond to Autoconf/Automake syntax:

(progname)_LDADD=(libname)

3. define BOTH compiler search paths and linker search paths in Project build>Search directories tab > Compiler and Linker tabs

this corresponds in turn to:

(progname)_LDFLAGS=(-L flags) (-l flags)
and to:
(progname)_CPPFLAGS=(-I lfags)

Check that you did all 3 steps first. Also, check that you did it for the same target all times (my frequent mistake is to do it for the Release target and at other times for the Debug target inconsistently).
Now you may have an occasional bug too in your step 2. If you used the file-dialog window, the resulting relative names may be occasionally wrong (inconsistent number of ../), an issue that I inconsistently experienced without being able to pinpoint a pattern systematic enough to be posted on the forum. If so, my advice would be to use absolute filenames, possibly typed by hand in the edit window.

If this does not work, simply edit your .cbp file and correct the paths by hand...
good luck, Fab
« Last Edit: June 22, 2008, 05:55:07 pm by libfab »

Offline Vinther

  • Single posting newcomer
  • *
  • Posts: 4
Re: Error when linking custom libraries
« Reply #2 on: June 22, 2008, 04:52:42 pm »
Hmm, I'm pretty sure I've done all this. And after reading your post I went over it all once again, so I shouldn't have missed anything.
I don't know what Autoconf or that stuff you wrote is, but I guess I did the same things through the menus.

This is the first time I've ever tried to write a library, so maybe I'm doing something wrong in the code itself. But I figure it's just like any program, but without a 'main' function, so it's more like a collection of classes and functions, compiled as a 'static library' to produce the *.a file.

If it helps in any way I'll just post my system information and the build log here.

Windows XP Pro x64 SP2, Code::Blocks 8.02, MinGW GCC, Allegro 4.2.2

One thing I think looks suspicious here, is that it never says anything about linking the allegro library (liballeg.a)
Code
-------------- Build: Debug in GAME CORE ---------------

Compiling: src\INPUT.cpp
Compiling: src\CORE.cpp
Linking static library: lib\Debug\libgamecore.a
ar.exe: creating lib\Debug\libgamecore.a
Output size is 104.56 KB

-------------- Build: Debug in GUI ---------------

Compiling: src\GUI_BUTTON.cpp
Compiling: src\GUI_CANVAS.cpp
Compiling: src\GUI.cpp
Linking static library: lib\Debug\libgui.a
ar.exe: creating lib\Debug\libgui.a
Output size is 164.96 KB

-------------- Build: Debug in VGFX ---------------

Compiling: src\POLYGON.cpp
Compiling: src\SKELETON.cpp
Compiling: src\VECTOR.cpp
Compiling: src\VGFX.cpp
Compiling: src\MODEL.cpp
Linking static library: lib\Debug\libvgfx.a
ar.exe: creating lib\Debug\libvgfx.a
Output size is 121.12 KB

-------------- Build: Debug in Vector Editor ---------------

Compiling: src\main.cpp
Linking console executable: bin\Debug\veditor.exe
D:\Projects\Programming\GAME CORE\lib\Debug\libgamecore.a(CORE.o): In function `_ZN4CORED2Ev':
D:/Projects/Programming/GAME CORE/src/CORE.cpp:18: undefined reference to `_destroy_bitmap'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:19: undefined reference to `_destroy_bitmap'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:23: undefined reference to `_allegro_exit'
D:\Projects\Programming\GAME CORE\lib\Debug\libgamecore.a(CORE.o): In function `_ZN4CORED1Ev':
D:/Projects/Programming/GAME CORE/src/CORE.cpp:18: undefined reference to `_destroy_bitmap'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:19: undefined reference to `_destroy_bitmap'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:23: undefined reference to `_allegro_exit'
D:\Projects\Programming\GAME CORE\lib\Debug\libgamecore.a(CORE.o): In function `_ZN4CORED0Ev':
D:/Projects/Programming/GAME CORE/src/CORE.cpp:18: undefined reference to `_destroy_bitmap'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:19: undefined reference to `_destroy_bitmap'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:23: undefined reference to `_allegro_exit'
D:\Projects\Programming\GAME CORE\lib\Debug\libgamecore.a(CORE.o): In function `_ZN4CORE10initializeEv':
D:/Projects/Programming/GAME CORE/src/CORE.cpp:29: undefined reference to `__install_allegro_version_check'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:30: undefined reference to `_install_keyboard'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:31: undefined reference to `_install_mouse'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:34: undefined reference to `_request_refresh_rate'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:35: undefined reference to `_set_gfx_mode'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:39: undefined reference to `_create_video_bitmap'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:40: undefined reference to `_create_video_bitmap'
D:\Projects\Programming\GAME CORE\lib\Debug\libgamecore.a(CORE.o): In function `_ZN4CORE13draw_graphicsEv':
D:/Projects/Programming/GAME CORE/src/CORE.cpp:61: undefined reference to `_clear_bitmap'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:69: undefined reference to `_show_video_bitmap'
D:/Projects/Programming/GAME CORE/src/CORE.cpp:72: undefined reference to `_rest'
D:\Projects\Programming\GAME CORE\lib\Debug\libgamecore.a(INPUT.o): In function `_ZN5INPUT6updateEv':
D:/Projects/Programming/GAME CORE/src/INPUT.cpp:16: undefined reference to `__imp__key'
D:/Projects/Programming/GAME CORE/src/INPUT.cpp:19: undefined reference to `__imp__mouse_x'
D:/Projects/Programming/GAME CORE/src/INPUT.cpp:20: undefined reference to `__imp__mouse_y'
D:/Projects/Programming/GAME CORE/src/INPUT.cpp:22: undefined reference to `__imp__mouse_b'
D:\Projects\Programming\GUI\lib\Debug\libgui.a(GUI_BUTTON.o): In function `_ZN10GUI_BUTTON4drawEP6BITMAP':
D:/Projects/Programming/GUI/src/GUI_BUTTON.cpp:56: undefined reference to `_textout_centre_ex'
D:\Projects\Programming\GUI\lib\Debug\libgui.a(GUI_CANVAS.o): In function `_ZN10GUI_CANVASC2Eii':
D:/Projects/Programming/GUI/src/GUI_CANVAS.cpp:11: undefined reference to `_create_bitmap'
D:\Projects\Programming\GUI\lib\Debug\libgui.a(GUI_CANVAS.o): In function `_ZN10GUI_CANVASC1Eii':
D:/Projects/Programming/GUI/src/GUI_CANVAS.cpp:11: undefined reference to `_create_bitmap'
D:\Projects\Programming\GUI\lib\Debug\libgui.a(GUI_CANVAS.o): In function `_ZN10GUI_CANVASD2Ev':
D:/Projects/Programming/GUI/src/GUI_CANVAS.cpp:21: undefined reference to `_destroy_bitmap'
D:\Projects\Programming\GUI\lib\Debug\libgui.a(GUI_CANVAS.o): In function `_ZN10GUI_CANVASD1Ev':
D:/Projects/Programming/GUI/src/GUI_CANVAS.cpp:21: undefined reference to `_destroy_bitmap'
D:\Projects\Programming\GUI\lib\Debug\libgui.a(GUI_CANVAS.o): In function `_ZN10GUI_CANVASD0Ev':
D:/Projects/Programming/GUI/src/GUI_CANVAS.cpp:21: undefined reference to `_destroy_bitmap'
D:\Projects\Programming\GUI\lib\Debug\libgui.a(GUI_CANVAS.o): In function `_ZN10GUI_CANVAS4drawEP6BITMAP':
D:/Projects/Programming/GUI/src/GUI_CANVAS.cpp:29: undefined reference to `_blit'
D:\Projects\Programming\VGFX\lib\Debug\libvgfx.a(VGFX.o): In function `_ZN4VGFX6renderEv':
D:/Projects/Programming/VGFX/src/VGFX.cpp:52: undefined reference to `_clear_bitmap'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 1 seconds)
31 errors, 0 warnings

Offline libfab

  • Multiple posting newcomer
  • *
  • Posts: 45
Re: Error when linking custom libraries
« Reply #3 on: June 22, 2008, 05:59:33 pm »
Vinther,
Just upload your Build log (the text in the window pane that is generated on building your app), your workspace and .cbp project files -- I'm clueless here.
Fab

Offline Vinther

  • Single posting newcomer
  • *
  • Posts: 4
Re: Error when linking custom libraries
« Reply #4 on: June 22, 2008, 06:50:45 pm »
Ok, here they are. The build log is the one in my previous post.
Thank you so much for taking the time :)

Vector Editor.workspace
Code
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_workspace_file>
<Workspace title="Workspace">
<Project filename="Vector Editor.cbp" active="1">
<Depends filename="..\GAME CORE\GAME CORE.cbp" />
<Depends filename="..\GUI\GUI.cbp" />
<Depends filename="..\VGFX\VGFX.cbp" />
</Project>
<Project filename="..\GAME CORE\GAME CORE.cbp" />
<Project filename="..\GUI\GUI.cbp">
<Depends filename="..\GAME CORE\GAME CORE.cbp" />
</Project>
<Project filename="..\VGFX\VGFX.cbp" />
</Workspace>
</CodeBlocks_workspace_file>

Vector Editor.cbp
Code
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Vector Editor" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin\Debug\veditor" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Debug\" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\Vector Editor\include" />
<Add directory="D:\Projects\Programming\GAME CORE\include" />
<Add directory="D:\Projects\Programming\GUI\include" />
<Add directory="D:\Projects\Programming\VGFX\include" />
</Compiler>
<Linker>
<Add library="C:\Program Files (x86)\CodeBlocks\MinGW\lib\liballeg.a" />
<Add library="D:\Projects\Programming\GAME CORE\lib\Debug\libgamecore.a" />
<Add library="D:\Projects\Programming\GUI\lib\Debug\libgui.a" />
<Add library="D:\Projects\Programming\VGFX\lib\Debug\libvgfx.a" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\lib" />
<Add directory="D:\Projects\Programming\GAME CORE\lib\Debug" />
<Add directory="D:\Projects\Programming\GUI\lib\Debug" />
<Add directory="D:\Projects\Programming\VGFX\lib\Debug" />
</Linker>
</Target>
<Target title="Release">
<Option output="bin\Release\veditor" prefix_auto="1" extension_auto="1" />
<Option object_output="obj\Release\" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\GAME CORE\include" />
<Add directory="D:\Projects\Programming\GUI\include" />
<Add directory="D:\Projects\Programming\VGFX\include" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\Vector Editor\include" />
<Add directory="D:\Projects\Programming\GAME CORE\include" />
<Add directory="D:\Projects\Programming\GUI\include" />
<Add directory="D:\Projects\Programming\VGFX\include" />
</Compiler>
<Linker>
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\lib" />
<Add directory="D:\Projects\Programming\GAME CORE\lib\Debug" />
<Add directory="D:\Projects\Programming\GUI\lib\Debug" />
<Add directory="D:\Projects\Programming\VGFX\lib\Debug" />
</Linker>
<Unit filename="src\main.cpp" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

GAME CORE.cbp
Code
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="GAME CORE" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="lib\Debug\libgamecore" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="obj\Debug\" />
<Option type="2" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\GAME CORE\include" />
</Compiler>
<Linker>
<Add library="C:\Program Files (x86)\CodeBlocks\MinGW\lib\liballeg.a" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\lib" />
</Linker>
</Target>
<Target title="Release">
<Option output="lib\Release\libgamecore" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="obj\Release\" />
<Option type="2" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\GAME CORE\include" />
</Compiler>
<Linker>
<Add option="-s" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\lib" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\GAME CORE\include" />
</Compiler>
<Linker>
<Add library="C:\Program Files (x86)\CodeBlocks\MinGW\lib\liballeg.a" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\lib" />
</Linker>
<Unit filename="include\CORE.h" />
<Unit filename="include\INPUT.h" />
<Unit filename="src\CORE.cpp" />
<Unit filename="src\INPUT.cpp" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

GUI.cbp
Code
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="GUI" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="lib\Debug\libgui" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="obj\Debug\" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="-g" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\GAME CORE\include" />
<Add directory="D:\Projects\Programming\GUI\include" />
</Compiler>
<Linker>
<Add library="C:\Program Files (x86)\CodeBlocks\MinGW\lib\liballeg.a" />
<Add library="D:\Projects\Programming\GAME CORE\lib\Debug\libgamecore.a" />
<Add directory="D:\Projects\Programming\GAME CORE\lib\Debug" />
</Linker>
</Target>
<Target title="Release">
<Option output="lib\Release\libgui" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="obj\Release\" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="-O2" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\GAME CORE\include" />
<Add directory="D:\Projects\Programming\GUI\include" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\GAME CORE\include" />
<Add directory="D:\Projects\Programming\GUI\include" />
</Compiler>
<Linker>
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\lib" />
</Linker>
<Unit filename="include\GUI.h" />
<Unit filename="include\GUI_BUTTON.h" />
<Unit filename="include\GUI_CANVAS.h" />
<Unit filename="src\GUI.cpp" />
<Unit filename="src\GUI_BUTTON.cpp" />
<Unit filename="src\GUI_CANVAS.cpp" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

VGFX.cbp
Code
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="VGFX" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="lib\Debug\libvgfx" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="obj\Debug\" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="-g" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\VGFX\include" />
</Compiler>
<Linker>
<Add library="C:\Program Files (x86)\CodeBlocks\MinGW\lib\liballeg.a" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\lib" />
</Linker>
</Target>
<Target title="Release">
<Option output="lib\Release\libvgfx" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="obj\Release\" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="-O2" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\VGFX\include" />
</Compiler>
<Linker>
<Add option="-s" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\lib" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\include" />
<Add directory="D:\Projects\Programming\VGFX\include" />
</Compiler>
<Linker>
<Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\lib" />
</Linker>
<Unit filename="include\MODEL.h" />
<Unit filename="include\POLYGON.h" />
<Unit filename="include\SKELETON.h" />
<Unit filename="include\VECTOR.h" />
<Unit filename="include\VGFX.h" />
<Unit filename="src\MODEL.cpp" />
<Unit filename="src\POLYGON.cpp" />
<Unit filename="src\SKELETON.cpp" />
<Unit filename="src\VECTOR.cpp" />
<Unit filename="src\VGFX.cpp" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

Offline libfab

  • Multiple posting newcomer
  • *
  • Posts: 45
Re: Error when linking custom libraries
« Reply #5 on: June 22, 2008, 08:33:17 pm »
First off,
in GUI.cbp you get:
<Linker>
               <Add library="C:\Program Files (x86)\CodeBlocks\MinGW\lib\liballeg.a" />
               <Add library="D:\Projects\Programming\GAME CORE\lib\Debug\libgamecore.a" />
               <Add directory="D:\Projects\Programming\GAME CORE\lib\Debug" />
</Linker>
OK? There's something missing on the face of it, like:
                                       <Add directory="C:\Program Files (x86)\CodeBlocks\MinGW\lib" />
Second, if this does not cure the issue, rename the file path
                                      C:\Program Files (x86)\CodeBlocks\MinGW\lib
like this:
                                      C:\Program Files_(x86)\CodeBlocks\MinGW\lib

CB has issues with whitespace here and there (I spotted one recently in the console Run menu).
Best, Fab




Offline Vinther

  • Single posting newcomer
  • *
  • Posts: 4
Re: Error when linking custom libraries
« Reply #6 on: June 22, 2008, 10:29:55 pm »
Ok, now I went through all of the .cbp files, making sure all of the libraries and directories were added for both the Release and Debug targets, and for the projects themselves...
And it bloody worked! I didn't even have to mess with the spaces in the paths.
Libfab, I could kiss you :D

I guess it was all just because of me being a bit sloppy when I entered all of the paths in CB, before. But it was kind of hard to keep track of everything in there...

Oh, well. Thank you!