User forums > Using Code::Blocks

Can't successfully link library

(1/1)

trino:
Hi all,

I'm runnign codeblocks 12.11 on Ubuntu 13.10 using gcc 4.8.1

I was having some problems using one static library where I would get undefined reference errors to functions of this library. After some time I decided to create a small example with a simple library to see if the error was in linking the library.

So I created a project helloLib for a static library and defined only the following function on it:


--- Code: ---#include <stdio.h>

void printHello(){
    printf("Hello!");
}

--- End code ---

And also created a header called functions.h with the following prototype:


--- Code: ---void printHello();

--- End code ---

Then I created another project as console application and made the following configurations in order to link helloLib to it:


--- Quote ---Project build options >> Link Settings >> Link libraries >> added helloLib
Project build options >> Search directories >> Compiler >> added the path to the folder containing functions.h
Project build options >> Search directories >> Linker >> added the path to the folder containing libhelloLib.a

--- End quote ---

Then I edited the main.cpp file of this project like this:


--- Code: ---#include <iostream>

#include <functions.h>

using namespace std;

int main(){
    printHello();

    cout << "Hello world!" << endl;
    return 0;
}

--- End code ---

Now when I try to compile this I get the same kind of error:

Build Log:

--- Code: ---g++ -L/home/user/helloLib  -o bin/Debug/libraryTest obj/Debug/main.o    -lhelloLib
obj/Debug/main.o: In function `main':
/home/user/libraryTest/main.cpp:8: undefined reference to `printHello()'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings (0 minutes, 0 seconds)

--- End code ---

Can someone help me with that?

Thanks.

scarphin:
Are you sure that's not related to mixing C and C++, like definition of 'extern "C"' missing somewhere?

trino:
aaaaah, It was missing one in the header of the library

Thank you.

Navigation

[0] Message Index

Go to full version