Author Topic: Linking to Libraries  (Read 12133 times)

Offline racer319

  • Multiple posting newcomer
  • *
  • Posts: 10
Linking to Libraries
« on: May 01, 2012, 10:08:01 pm »
Windows XP, Code::Blocks 10.05

I'm using SDL and it's working fine, but now i'm trying to implement the SDL_gfx library. I downloaded it, extracted it, and put all the header/cpp files into a shared library project in CB and built it. In the release folder there were three files: libSDL_gfx.dll, libSDL_gfx.dll (A File), and libSDL_gfx.dll (Export Definition File).

I put the first into the SDL bin folder, next to SDL.dll. the second in the SDL lib folder next to libSDLmain and libSDL.dll. the third one i didn't do anything with. I then took all the header files that came with the download and put them in my SDL include folder.

When I compile, it works, but when I try to use a function from the library (pixelRGBA) i get the error: undefinded reference to 'pixelRGBA'

I'm quite sure I did something horribly stupid. Could someone tell me where I went wrong? Thanks.
Windows XP, GNU GCC Compiler, Code::Blocks 10.05

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline racer319

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Linking to Libraries
« Reply #2 on: May 01, 2012, 11:19:18 pm »
I've done that.

Link libraries:
SDL_gfx.dll

Search Directories -> Linker:

C:/SDL/lib

Did I build the library wrong?
Windows XP, GNU GCC Compiler, Code::Blocks 10.05

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Linking to Libraries
« Reply #3 on: May 02, 2012, 12:37:53 am »
Remove .dll in SDL_gfx and try again.

Also check with a tool capable of reading dlls if the missing symbols are in your library.
One such tool is dependency walker.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Linking to Libraries
« Reply #4 on: May 02, 2012, 01:25:21 am »
I strongly suggest turning on full compiler logging
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

Note: With some linker options the order of options matter.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline racer319

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Linking to Libraries
« Reply #5 on: May 02, 2012, 01:28:09 am »
No luck with removing the dll. And I'm not really sure how to use Dependency Walker. I opened the dll with it but what do I do then?

Also, is linking to the library all I need to do? Or do I also need a header file. b/c I've been including the SDL_gfxPrimitives.h header in my project which has the line:

Code
SDL_GFXPRIMITIVES_SCOPE int pixelRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

In it, which is the function I'm trying to use, but it's not really a whole function. In the .cpp file the whole function is there but there's no way to link to it. It should be part of the library though right?

I turned on full compiler logging. Here's the error:

Code
\SDL\lib\libSDL_gfx.dll.a 
obj\Debug\main.o: In function `SDL_main':
C:/SDLtest/main.cpp:38: undefined reference to `pixelRGBA'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 4 seconds)
1 errors, 234 warnings
Windows XP, GNU GCC Compiler, Code::Blocks 10.05

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Linking to Libraries
« Reply #6 on: May 02, 2012, 01:39:04 am »
Please post the linking command; Edit3: This will be in the "Build Log" NOT IN the "Build Messages".

Note: This is not a valid Code::Blocks topic; so, the thread may be locked with no warning.

Edit: Including a header fixes Compiler Errors/Warning.
Adding the correct library to the linker command fixes linker errors like you have.

Edit2: Errors with "undefined reference" are always linker errors in my past experience.

Tim S.
« Last Edit: May 02, 2012, 01:48:16 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline racer319

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Linking to Libraries
« Reply #7 on: May 02, 2012, 01:52:16 am »
Oh okay sorry. Where should I have posted this?

When I change SDL_gfx to SDL_gfx.dll in my linker the error changes to 'was not declared in this scope'. which should I use?
Windows XP, GNU GCC Compiler, Code::Blocks 10.05

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Linking to Libraries
« Reply #8 on: May 02, 2012, 02:10:44 am »
Oh okay sorry. Where should I have posted this?

Once you get the Full Compiler Logging working; you can/should post the question on a programing, library, or compiler support site.

Programming in C or C++ Support site: http://cboard.cprogramming.com/

No idea which compiler you are using.

If MinGW GCC, I know of no good support site for it. Note: cboard.cprogramming.com does answer some Compiler support questions.

Tim S.
« Last Edit: May 02, 2012, 02:19:24 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Linking to Libraries
« Reply #9 on: May 02, 2012, 02:19:07 am »
When I change SDL_gfx to SDL_gfx.dll in my linker the error changes to 'was not declared in this scope'. which should I use?

Without the full build log; there is no way I can help you.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline racer319

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Linking to Libraries
« Reply #10 on: May 02, 2012, 02:33:27 am »
(Cropped out the warnings b/c it exceeded max post length)

Code
-------------- Build: Debug in Space_Shooter ---------------

mingw32-g++.exe -Wall  -g    -IC:\GameDev\SDL\include -I..\..\..\SDL\include\SDL  -c C:\GameDev\C++_Source_Files\Misc\Space_Shooter\main.cpp -o obj\Debug\main.o
C:\GameDev\C++_Source_Files\Misc\Space_Shooter\main.cpp: In function 'int SDL_main(int, char**)':
C:\GameDev\C++_Source_Files\Misc\Space_Shooter\main.cpp:37: error: 'pixelRGBA' was not declared in this scope
C:\GameDev\C++_Source_Files\Misc\Space_Shooter\main.cpp:13: warning: unused variable 'grey'
Process terminated with status 1 (0 minutes, 2 seconds)
1 errors, 234 warnings
 
Windows XP, GNU GCC Compiler, Code::Blocks 10.05

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Linking to Libraries
« Reply #11 on: May 02, 2012, 03:13:39 am »
(Cropped out the warnings b/c it exceeded max post length)

Code
-------------- Build: Debug in Space_Shooter ---------------

mingw32-g++.exe -Wall  -g    -IC:\GameDev\SDL\include -I..\..\..\SDL\include\SDL  -c C:\GameDev\C++_Source_Files\Misc\Space_Shooter\main.cpp -o obj\Debug\main.o
C:\GameDev\C++_Source_Files\Misc\Space_Shooter\main.cpp: In function 'int SDL_main(int, char**)':
C:\GameDev\C++_Source_Files\Misc\Space_Shooter\main.cpp:37: error: 'pixelRGBA' was not declared in this scope
C:\GameDev\C++_Source_Files\Misc\Space_Shooter\main.cpp:13: warning: unused variable 'grey'
Process terminated with status 1 (0 minutes, 2 seconds)
1 errors, 234 warnings
 

I need to see the linker command; the "mingw32-g++.exe -Wall  -g" means that is the compiler command or you have a very very messed up Code::Blocks configuration.

NOTE: you did NOT do something stupid like remove the include statement needed to get the code to compile, did you?

Please fix your code so it compiles; then post the link command.

Edit: Going to sleep; I suggest trying another forum; hopefully one that supports your SDL library.

Tim S.
« Last Edit: May 02, 2012, 03:30:11 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline racer319

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: Linking to Libraries
« Reply #12 on: May 02, 2012, 03:50:31 am »
Thanks a bunch for the help though. And yeah I probably did do something stupid...
Windows XP, GNU GCC Compiler, Code::Blocks 10.05

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Linking to Libraries
« Reply #13 on: May 02, 2012, 06:34:43 am »
And think about name mangling, if you probably use C-libraries with C++-code.

Offline Radek

  • Multiple posting newcomer
  • *
  • Posts: 104
Re: Linking to Libraries
« Reply #14 on: May 02, 2012, 06:48:31 am »
You are getting compiler errors, not linker errors. The problem is not related to your DLLs, at least, so far. A header with "pixelRGBA" declaration has not been found. Check your (project) include path, whether it contains needed headers. Check whether the needed header is present. Also, get rid of those 234 warnings.

Linker errors (which are usually related to missing DLLs, missing libraries or unexpected kind of name mangling) are the complaints of "unresolved reference" or "duplicate reference" in compiled files (*.obj or *.o, not in *.cpp).

modal

  • Guest
Re: Linking to Libraries
« Reply #15 on: October 13, 2012, 06:37:57 am »
I had the same problem. The problem is the compile line be sure
to also include (without backquotes) something like: -lSDL_gfx    e.g. here is my build line from Geany:
gcc -Wall  `sdl-config --cflags --libs` -lSDL_gfx -lm -o "%e" "%f";
where %e is the compiled name and %f is the object file name.  Seems the documentation is fragmented.
One alternative is cairo graphics for better control, smoothing, and registered primitives. :D
In example I found they had included type bool, not in GNU C! Quick fix:  int true=1,false=0;
 :)          :)          :)          :)          :)          :)          :)          :)          :)          :)