Author Topic: Can't successfully link library  (Read 3315 times)

Offline trino

  • Single posting newcomer
  • *
  • Posts: 4
Can't successfully link library
« on: December 18, 2013, 04:05:18 pm »
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!");
}

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

Code
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:

Code
#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:
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)

Can someone help me with that?

Thanks.

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: Can't successfully link library
« Reply #1 on: December 18, 2013, 04:38:15 pm »
Are you sure that's not related to mixing C and C++, like definition of 'extern "C"' missing somewhere?

Offline trino

  • Single posting newcomer
  • *
  • Posts: 4
Re: Can't successfully link library
« Reply #2 on: December 18, 2013, 05:01:08 pm »
aaaaah, It was missing one in the header of the library

Thank you.