Author Topic: How to Link to Static Lib Example?  (Read 9484 times)

mejohnsn

  • Guest
How to Link to Static Lib Example?
« on: March 01, 2006, 09:40:50 pm »
Hi-

I got the static library example (File:New Project:Static Library) to build easily enough, but now I tried to actually use the library, and the best I can get is "undefined reference to 'SampleAddInt(int, int)'".

I got this far after making a header file, 'StaticLibEx.h' and adding it to a new project, titled "Console Application" (made using File:New Project:Console Application and editing to include 'StaticLibEx.h' and a call to SampleAddInt) and adding the absolute file name (including path & extension) to the newly made library in (right-click on) Console Application:Build Options:Linker:Link libraries.

I even moved all of the above to directories with no spaces in them just to make sure that spaces in path names wasn't causing the problem. And this move _did_ get certain other error messages to go away.

So what have I left out? Why can't it find the file d:\MyCodeBlocksProjects\StaticLibEx\libstaticlib.a at link time?  Windows Explorer can find it.

I have MinGW installed and I have never modified the defaults in Settings:Compiler:Advanced Options.

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: How to Link to Static Lib Example?
« Reply #1 on: March 01, 2006, 09:54:07 pm »
Hello,

May be this topic could be useful for you.

Best wishes,
Michael

mejohnsn

  • Guest
Re: How to Link to Static Lib Example?
« Reply #2 on: March 02, 2006, 12:33:22 am »
Thanks for the reference.

I can see that the OP in the thread you gave me is facing the same symptoms at least since reply#8. I haven't read his project files yet, so I don't know for sure that it is exactly the same, but we both get the identical 'undefined reference'. I also see that the OP is convinced that CB is finding the library, but I am not sure I should agree.

And why _does_ it say 'SOLVED', when the OP is still having the problem in reply#13?  Did the OP figure out the solution and then never post his project files? If so, then no, there is nothing in that thread that is helpful to me.

Finally, he says to click on http://pastebin.com/575053 to see the code, but I get a blank CGI form when I do that. So I can't even compare the code.

My code, OTOH, is ridiculously simple, so I should not need to zip it. It is:

#include <iostream>
#include "StaticLibEx.h" // Not sure how it found it,  but it found it.
using namespace std;

int main()
{
    int i=5, j=3;
   cout << "Hello world!" << endl;
   cout << "Testing Static Lib example:" << endl;
   int lib_return = SampleAddInt(i,j);   // error message here, as if lib nt fnd or MT
   cout << "SampleAddInt(5,3) returns " << lib_return << endl;

   return 0;
}

And this code is in a separate project, titled "Console Application" (created using Project:New Project:Console Application) with no changes, except that the directory was changed to keep from overwriting 'main.cpp' in other projects.

Finally, the source and project for Static Lib are unmodified from the default created by using Project:New Project:Static Library


Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: How to Link to Static Lib Example?
« Reply #3 on: March 02, 2006, 12:39:57 am »
This issue is not a bug or a problem in Code::Blocks, which is merely a graphical interface for the GCC compiler; rather, it is with the way GCC is used.

Posting the command lines Code::Blocks uses to compile will highlight your problem. If you haven't already, change "Compiler logging" to "Full command line" in the Other tab of your global compiler settings, then paste the build log for both projects.
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

mejohnsn

  • Guest
Re: How to Link to Static Lib Example?
« Reply #4 on: March 02, 2006, 01:34:51 am »
Thank you, Tdragon, for that very helpful hint. I turned it on, and I get:

Project   : Console application
Compiler  : GNU GCC Compiler (called directly)
Directory : D:\MyCodeBlocksProjects\StaticLibEx\
--------------------------------------------------------------------------------
Switching to target: default
mingw32-g++.exe   -Ld:\MyCodeBlocksProjects\StaticLibEx\  -L"D:\Program Files\CodeBlocks\lib" -o d:\MyCodeBlocksProjects\StaticLibEx\StaticLibusr.exe .objs\main.o        d:\MyCodeBlocksProjects\StaticLibEx\libstaticlib.a
.objs\main.o: In function `main':
D:/MyCodeBlocksProjects/StaticLibEx/main.cpp:10: undefined reference to `SampleAddInt(int, int)'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings


While I am waiting for the group's feedback, I am going to investigate where that "-Ld:\MyCodeBlocksProjects\StaticLibEx\ " came from, since I don't think it should be there at the same time as "d:-MyCode...\libstaticlib.a", and I thought I turned that Project:Build Options:Directory option off some time ago.

Come to think of it, isn't there a space or '"' missing in that -L option? I found where it came from: it came from (rt clk on project) Console Application:Build Options:Linker:Link Libraries, which I am now blanking out before retrying to get the same thing. Then I found it _also_ under (rt clk on project) Console Application:Build Options:Directories and deleted it there.

Now I have a compiler command line that looks (to me) more sensible, namely:
Switching to target: default
mingw32-g++.exe    -L"D:\Program Files\CodeBlocks\lib" -o d:\MyCodeBlocksProjects\StaticLibEx\StaticLibusr.exe .objs\main.o

But the rest of the log output still shows the same failure, namely:
       
.objs\main.o: In function `main':
D:/MyCodeBlocksProjects/StaticLibEx/main.cpp:10: undefined reference to `SampleAddInt(int, int)'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings

BTW: I don't see the "-L" option listed in mingw32-g++.exe --help, so I am still guessing when I say this last log output looks better.

Again, thanks for the tip about where the option to turn on this log output is.

Offline Game_Ender

  • Lives here!
  • ****
  • Posts: 551
Re: How to Link to Static Lib Example?
« Reply #5 on: March 02, 2006, 03:15:26 am »
Here is how the command line should look:

Code
g++ -L"C:\static\lib\directory" main.o -o staticlibusr.exe  -lmystaticlib

You would get this by adding "C:\static\lib\directory" to the linker directories (important).  Then adding "mystaticlib" to the link libraries.

mejohnsn

  • Guest
Re: How to Link to Static Lib Example?
« Reply #6 on: March 02, 2006, 03:45:31 am »
Hi again y'all-

I just noticed that the final log output in my previous post is missing the reference to the library. Which certainly explains why _it_ fails. But it still does not explain why the previous attempt also failed, with a compiler command that had such a suspicous looking -L argument.

So now I restored the reference to the static library in (rt click on)Console Application:Build Options:Linker:Link Libraries, to finally get a command line that makes the most sense of all to me. But this too fails with the same error message:-(

The log output this time is:
Switching to target: default
mingw32-g++.exe    -L"D:\Program Files\CodeBlocks\lib" -o d:\MyCodeBlocksProjects\StaticLibEx\StaticLibusr.exe .objs\main.o        D:\MyCodeBlocksProjects\StaticLibEx\libstaticlib.a
.objs\main.o: In function `main':
D:/MyCodeBlocksProjects/StaticLibEx/main.cpp:10: undefined reference to `SampleAddInt(int, int)'

I say this "makes the most sense to me", because I now see quote marks in the -L command, and I see both main.o and libstaticlib.a listed as object files(?). But I do not know mingw so well as to know for sure that that syntax is correct: I was relying on C::B to make it unnecessary for me to learn that level of detail;)

Does this help clarify what went wrong?

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: How to Link to Static Lib Example?
« Reply #7 on: March 02, 2006, 05:25:27 am »
Some thoughts:

Your command line should be either
mingw32-g++.exe -L"D:\Program Files\CodeBlocks\lib" -o d:\MyCodeBlocksProjects\StaticLibEx\StaticLibusr.exe .objs\main.o D:\MyCodeBlocksProjects\StaticLibEx\libstaticlib.a
or
mingw32-g++.exe -L"D:\Program Files\CodeBlocks\lib" -Ld:\MyCodeBlocksProjects\StaticLibEx -o d:\MyCodeBlocksProjects\StaticLibusr.exe .objs\main.o -lstaticlib

Both projects must be entirely in C (.c source file extension), or if the final executable is in C++ (.cpp source file extension) it must surround declarations of C functions with the extern "C" declaration.
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

mejohnsn

  • Guest
Re: How to Link to Static Lib Example?
« Reply #8 on: March 02, 2006, 06:18:35 am »
Thanks, TDragon!

You wrote:

>Some thoughts:

>Your command line should be either
>mingw32-g++.exe -L"D:\Program Files\CodeBlocks\lib" -o >d:\MyCodeBlocksProjects\StaticLibEx\StaticLibusr.exe .objs\main.o >D:\MyCodeBlocksProjects\StaticLibEx\libstaticlib.a
>or
>mingw32-g++.exe -L"D:\Program Files\CodeBlocks\lib" -Ld:\MyCodeBlocksProjects\StaticLibEx -o >d:\MyCodeBlocksProjects\StaticLibusr.exe .objs\main.o -lstaticlib

This is what I now have.


>Both projects must be entirely in C (.c source file extension),

Now that is the surprise to me, since I was using the sample projects in Project:New Project, one of which is C++ and the other C.

> or if the final executable is in C++ (.cpp source file extension)

It is, as you can see from the source code I posted earlier.

>it must surround declarations of C functions >with the extern "C" declaration.

That explains the error message perfectly! Thanks you. The fog lifts at last.

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: How to Link to Static Lib Example?
« Reply #9 on: March 02, 2006, 03:11:16 pm »
You're welcome. :)

To explain further: most C++ compilers are "link backwards-compatible" with C, provided that you account for "name mangling". This is a process in C++ whereby the name and arguments of a function are combined to create a unique identifier for it. Surrounding declarations with extern "C" {} turns off name mangling to allow C functions and structures, which are not mangled by the C compiler, to be imported.
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)