Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: Decrius on April 11, 2008, 09:24:36 pm

Title: Linking error
Post by: Decrius on April 11, 2008, 09:24:36 pm
Hi,

I have 2 projects, a static library and a console application.

This is the code of the library:

imageloader.cpp
Code
// The functions contained in this file are pretty dummy
// and are included only as a placeholder. Nevertheless,
// they *will* get included in the static library if you
// don't remove them :)
//
// Obviously, you 'll have to write yourself the super-duper
// functions to include in the resulting library...
// Also, it's not necessary to write every function in this file.
// Feel free to add more files in this project. They will be
// included in the resulting library.

// A function adding two integers and returning the result
int SampleAddInt(int i1, int i2)
{
    return i1 + i2;
}

// A function doing nothing ;)
void SampleFunction1()
{
    // insert code here
}

// A function always returning zero
int SampleFunction2()
{
    // insert code here
   
    return 0;
}

imageloader.h
Code
int SampleAddInt(int, int);
void SampleFunction1();
int SampleFunction2();

Sources of the console application:

main.cpp
Code
#include <iostream>
#include <imageloader.h>

using namespace std;

int main()
{
    cout << SampleAddInt(1, 4) << endl;
    return 0;
}

I build the library, put it in a search directory and linked to it.
Error:

E:/C/test/main.cpp:8: undefined reference to `SampleAddInt(int, int)'

What could possibly be wrong?

Thanks,
Decrius
Title: Re: Linking error
Post by: Ceniza on April 11, 2008, 10:59:37 pm
Please enable full command line logging following Bloody Cake's signature and paste the output of the Build log here.

Quote from: Bloody Cake's signature
Logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
Title: Re: Linking error
Post by: Decrius on April 13, 2008, 05:37:26 pm
Quote
-------------- Build: Debug in test ---------------

mingw32-g++.exe -Wall -fexceptions  -g   -IE:\Packs\SLtest\include  -c E:\C\test\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -LE:\Packs\SLtest\lib  -o bin\Debug\test.exe obj\Debug\main.o    -limageloader -lmingw32
obj\Debug\main.o: In function `main':
E:/C/test/main.cpp:8: 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

SLtest is the dir the library is located in.
Title: Re: Linking error
Post by: MortenMacFly on April 14, 2008, 10:34:21 am
mingw32-g++.exe -LE:\Packs\SLtest\lib  -o bin\Debug\test.exe obj\Debug\main.o    -limageloader -lmingw32
...and where in this line do you link against the lib that contains the function?! Is it called "imageloader"?! Otherwise you should add the lib to the linker options in case you missed that. The linker can't know what libs to link against. You are responsible for telling him using the linker options of your target/project.
Your's sincerely, the Bloody Cake.
Title: Re: Linking error
Post by: Deschamps on April 14, 2008, 11:44:53 am
Quote from: Decrius
SLtest is the dir the library is located in.

The linker is looking for this library in 'E:\Packs\SLtest\lib', not in 'E:\Packs\SLtest'. Maybe, you've copied the library into the wrong folder.

Regards.
 
Title: Re: Linking error
Post by: Decrius on April 15, 2008, 09:06:21 am
Nope, its called libimageloader.a and is located in sltest/lib/

The only thing I did was:
- Make a static library project and build the example file that comes with it. Move the .a file to Packs/SLtest/lib/, and add that to the global search directories.
- Make a header file with the prototypes of the functions.
- Make a console library, link to to 'imageloader', include the header file, try to compile and it says 'undefined reference'.

I've re-done this several times, keep getting the same error, what if you guys do these steps, do you get the same error too?

Thank you for your time and effort :)
Derius
Title: Re: Linking error
Post by: MortenMacFly on April 15, 2008, 10:41:23 am
I've re-done this several times, keep getting the same error, what if you guys do these steps, do you get the same error too?
I have done it similar and it just works. I have attached a sample workspace for your convenience. Probably this will help you to get where you want. I does not have a header file... this content is directly inside the main application (I was too lazy to do the header file...)

[attachment deleted by admin]
Title: Re: Linking error
Post by: Decrius on April 15, 2008, 01:20:55 pm
Yes, your project works for me, thank you.

But how could it possibly work? When I build the App, it includes the .a file, but it's never created...I don't see it appear neither do I compile the library...how could that ever work? How could it know the functions if the library isn't compiled, and how can it find these lib files?

Thank you.
Title: Re: Linking error
Post by: MortenMacFly on April 15, 2008, 08:39:09 pm
how could that ever work? How could it know the functions if the library isn't compiled, and how can it find these lib files?
Trust me: the library *is* compiled. :D
I used a project dependency here - so the app project depends on the lib project which get's compiled automatically once you compile the app. Have a look int he "MyLib folder - there is the *.a lib created... for sure. If you create a workspace you can setup such dependencies between projects... there are also other ways to achieve such behaviour.
Title: Re: Linking error
Post by: Decrius on April 15, 2008, 10:14:02 pm
Are you sure? I don't see any .a file appear in the directories...=/

As for depencies, none of them is set AFAIK. App Project -> Project -> Properties -> Project's Dependencies -> box of Lib is not ticked...
Title: Re: Linking error
Post by: rhf on April 16, 2008, 01:55:44 am
I used a project dependency here
Part of the confusion may result from your lib_usage.zip file. The included  lib_usage.workspace file does not include the dependency relation. Perhaps when you closed and zipped the project files, you may have forgotten to explicitly save the workspace file. The dependency, which is stored in the workspace file, was therefore lost.

This issue showed up recently in another discussion as well -
http://forums.codeblocks.org/index.php/topic,8214.0.html
Maybe this is something that should be re-worked.
Thanks.

Edit:
wrong thread above: http://forums.codeblocks.org/index.php/topic,8224.msg61136.html#msg61136