News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

Can't successfully link library

Started by trino, December 18, 2013, 04:05:18 PM

Previous topic - Next topic

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:


#include <stdio.h>

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


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


void printHello();


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

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


#include <iostream>

#include <functions.h>

using namespace std;

int main(){
    printHello();

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


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

Build Log:

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)


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.