User forums > Help
How to Link to Static Lib Example?
mejohnsn:
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.
Michael:
Hello,
May be this topic could be useful for you.
Best wishes,
Michael
mejohnsn:
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
TDragon:
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.
mejohnsn:
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.
Navigation
[0] Message Index
[#] Next page
Go to full version