Author Topic: Linking multiple chain-dependent static libs  (Read 6811 times)

Offline Digited

  • Multiple posting newcomer
  • *
  • Posts: 20
Linking multiple chain-dependent static libs
« on: September 25, 2007, 08:59:48 am »
I have a nightmare-like problem with static libs' linking
windows xp sp2 pro, last night build of c::b, mingw-gcc 3.4.5

There's a console app (.exe) and a static lib, which uses OpenGL. Console app links my static lib and win&opengl static libs from gcc's lib dir, linking is successfull. (libwinmm, libgdi32, libopengl32, libglu32)

Successfull linking finishes, when I try to link my other static libs, also compiled with mingw - and no matter what lib - jpeg or glew or parts of my static lib (I made 4 from 1 to build changes faster). Linker doesn't find anything in cross-dependent static libs, no matter they are all linked in app' options and all directories are added...

All static libs are compiled and found at linking stage, but not implementations... Also there are problems with linking default libs like winmm and opengl - also undefined references, no matter they all linked successfully when I had only one static lib...

What is happening?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Linking multiple chain-dependent static libs
« Reply #1 on: September 25, 2007, 09:05:22 am »
The order you list the Libraries in matter; I have been up to long for me to remember which goes first for sure.
But, I think if Lib1 calls code in Lib2 then Lib1 must be listed before Lib2 in the Library list.

I normally Google for the missing reference and see which library it is in.

Note, you did export the stuff from your library right?

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 Digited

  • Multiple posting newcomer
  • *
  • Posts: 20
Re: Linking multiple chain-dependent static libs
« Reply #2 on: September 25, 2007, 10:26:10 am »
Linkage ordering is right, for sure.

What and how should I export from the static library?..

Again, situation:
1. app exe + static lib which uses opengl. app links static lib and libwinmm, libopengl32 and libglu32. All works.
2. app exe + static lib 1 + static lib 2 + static lib 3. 3 libs are made from one lib to build changes faster. app links all 3 libs and libwinmm, libopengl32 and libglu32. Nothing links, opengl too! What can it be?..

linkage order: libwinmm, libopengl32, libglu32, sl3, sl2, sl1 (as they depend on each other). Just absolutely nothing is linking, like there's no libs! They are, and paths are ok in 1 and 2, because bin dir is still the same...

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Linking multiple chain-dependent static libs
« Reply #3 on: September 25, 2007, 10:51:34 am »
Look up the keywords __declspec(dllexport)

I found this with Google
http://www.geocities.com/yongweiwu/stdcall.htm
http://sig9.com/node/35

I am far from expert on this, I suggest reading this and other sources.

From what I read you don't need to use __declspec(dllexport) on static Libraries. Note, I have had to remove this code to get code to compile and link as static Library for Code::Blocks in the past.

In other words, look at the missing methods and see if they are prefixed by an Macro that is defined as __declspec(dllexport) or __declspec(dllimport).

Tim S
« Last Edit: September 25, 2007, 11:02: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 stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Linking multiple chain-dependent static libs
« Reply #4 on: September 25, 2007, 10:59:47 am »
linkage order: libwinmm, libopengl32, libglu32, sl3, sl2, sl1 (as they depend on each other). Just absolutely nothing is linking, like there's no libs! They are, and paths are ok in 1 and 2, because bin dir is still the same...

Base your answer on just the first 3 missing references.
The missing references are defined in which library?
The missing references are being called from what library or file?

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 Digited

  • Multiple posting newcomer
  • *
  • Posts: 20
Re: Linking multiple chain-dependent static libs
« Reply #5 on: September 25, 2007, 11:46:13 am »
declspec - for dynamic libs, not static. Static lib is just a container of objs, not a compiled and linked executable binary, as exe or dll. There's no difference in how many static libs you have in addition to .exe or .dll - the same linking of objs is made at the end, it's just a method of splitting up the code for faster compilation, when code is oftenly modified.

Now I changed nothing, and linker doesn't find one of the existing libs. I've done with mingw, for sure. Maybe later, with 4.x.x

Topic can be closed.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Linking multiple chain-dependent static libs
« Reply #6 on: September 25, 2007, 01:19:43 pm »
I don't think you'll have more luck with a different compiler, but feel free to try. It may be that you accidentially fix what's wrong in your project if you switch to a different compiler.

Without knowing any details, it is impossible to tell of course, but at least two things that could be wrong come to my mind: 1. having built GLEW wrong (missing the required define, or omitting it in your project), and 2. having circular dependencies in your s1, s2, s3.

You are right that declspec is not needed for static libraries.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline Digited

  • Multiple posting newcomer
  • *
  • Posts: 20
Re: Linking multiple chain-dependent static libs
« Reply #7 on: October 30, 2007, 05:31:25 am »
I've forgot to write about solution...
After I've tried ms compiler, it showed a strange error in one of my static libs - with HWND in header. I tried different tricks, finally removed HWND by void*, all worked fine.
Of cource after that I tried to compile solution with mingw, but it failed with more strange errors in code that was compiling without errors before - for that I'm sure, becouse I changed nothing there since successful compiling. That was enough, so I moved to ms compiler on windows absolutely. In other words, if there was a hidden reason for all link errors, I just didn't find it.

Thanks for trying to help, stahta01, Thomas.

(p.s. now we're experimenting with D 1.00, dmd compiler - the language looks absolutely fantastic, it must have a dark side... Or must not?..)

Offline Digited

  • Multiple posting newcomer
  • *
  • Posts: 20
Re: Linking multiple chain-dependent static libs
« Reply #8 on: October 30, 2007, 08:02:14 am »
Linkers hate me. Prove:

D project, DigitalMars D compiler & linker, compiling static lib project:

-------------- Build: Release_Win32 in mylib ---------------
dmd.exe  -release -O -inline  -release -O -inline   -I..\..\ -I..\..\ -Ibin -IC:\dm\src\phobos -IC:\dmd\src\phobos  -c file1.d -of.\bin\obj\file1.obj
dmd.exe  -release -O -inline  -release -O -inline   -I..\..\ -I..\..\ -Ibin -IC:\dm\src\phobos -IC:\dmd\src\phobos  -c file2.d -of.\bin\obj\file2.obj
lib.exe  .\bin\mylib.lib .\bin\obj\file1.obj .\bin\obj\file2.obj
Digital Mars Librarian Version 8.02n
Copyright (C) Digital Mars 2000-2007 All Rights Reserved
http://www.digitalmars.com/ctg/lib.html
Error: .\bin\mylib.lib : No such file or directory
Process terminated with status 1 (0 minutes, 1 seconds)
0 errors, 0 warnings

Does anyone have an idea what's wrong? mylib.lib was not linked in .\bin dir...
« Last Edit: October 30, 2007, 08:05:50 am by Digited »

Offline Digited

  • Multiple posting newcomer
  • *
  • Posts: 20
Re: Linking multiple chain-dependent static libs
« Reply #9 on: October 30, 2007, 08:24:38 am »
Fixed: in spite of lib-linker command line description in its docs
"switches...
Zero or more of the following. If none of -c, -d or -x is specified, the default action is to add the objfiles to the library."
the D lib-linker (lib.exe) MUST get -c in command line to link a static lib.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Linking multiple chain-dependent static libs
« Reply #10 on: October 30, 2007, 08:49:06 am »
I tried different tricks, finally removed HWND by void*, all worked fine.
You probably know that already, but this is not a good thing to do. Although it surely works and does not matter for the finally generated machine code, it is very, very bad style, as it sabotages the compiler's natural way of type checking. There are good reasons why compilers insist on checking types.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline Digited

  • Multiple posting newcomer
  • *
  • Posts: 20
Re: Linking multiple chain-dependent static libs
« Reply #11 on: October 30, 2007, 12:53:32 pm »
Compilers aren't linkers... When linker says that HWND is an undefined symbol and HDC before it isn't, you just have to do something with it. I couldnt do anything, but make a void* hack... _stdcall everywhere, all libs included (rest of win-deps is linked without question).

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Linking multiple chain-dependent static libs
« Reply #12 on: October 30, 2007, 05:21:34 pm »
Quote
Compilers aren't linkers... When linker says that HWND is an undefined symbol
Well, that is not what you said:
Quote
After I've tried ms compiler, it showed a strange error in one of my static libs - with HWND in header.

Either way, HWND is a typedef, and I don't see how it could be a missing symbol unless you have screwed up something really, really bad. What I could imagine is a "type not declared in this scope" error at compile time.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."