1

I've been trying to link a simple static library to one of my c++ projects, but I can't seem to get it quite right. I know that this is a very widespread topic and that a lot of people have already asked a similar question, but even after reading some of the answers, I still cannot, for the love of god, figure out what I am doing wrong.

My code is very simple, first I have a .cpp source file titled "Math.cpp" that looks like this:

int max(int a, int b) {

    return a > b ? a : b;

}
int min(int a, int b) {

    return a < b ? a : b;

}
int floor(double a) {

    return (int) a;

}
int ceil(double a) {

    return (int) a + 1;

}

..And to go with that I made a header file called "Math.h" that looks like this:

#pragma once

int max(int, int);
int min(int, int);
int floor(double);
int ceil(double);

I then compile "Math.cpp" with the following command on cmd:

g++ -c Math.cpp -o Math.o

...and then compile it into a static library like so:

ar rcs libMath.a Math.o

After all of this I make a new c++ soure file titled "Main.cpp" that looks like this:

#include <iostream>
#include "Math.h"

int main() {

    std::cout << max(9, 8) << std::endl;
    return 0;

}

("Math.h" is in the same directory as "Main.cpp")

So finally in order to link "Main.cpp" with my static library ("libMath.a"), I use the following command in cmd:

g++ -o Main.exe Main.cpp -L. -lMath 

however, at this point, it throws the following error:

C:\Users\zalmar\AppData\Local\Temp\ccmOnvyg.o:Main.cpp:(.text+0x18): undefined reference to `int max<int>(int, int)'
collect2.exe: error: ld returned 1 exit status

... I cannot figure out why it can't find the reference to the specific function. Some people appeared to have the same problem (here for example). Their solution was to declare the Main.cpp source file before declaring the library path. However, that was not the case for me, even though I made sure I was linking the library after the Main.cpp it still came up with the same error. I would greatly appreciate it if someone could point out anything I might be doing wrong because clearly I must be doing something wrong. Otherwise it might be a problem with my MinGW compiler, maybe?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Spice
  • 216
  • 1
  • 3
  • 12

1 Answers1

1

You need to include the implementation of each of your functions in Math.h. Declare them inline, e.g.

template<typename T>
inline T max(T a, T b)
{
    return a > b ? a : b;
}

The reason is that when compiling your Math.cpp file, the compiler doesn't know which data types you want to instantiate the function for.

So, inline the functions in Math.h and get rid of Math.cpp.

Sid S
  • 6,037
  • 2
  • 18
  • 24
  • I tried that but it still seemed to produce the same error. – Spice Apr 15 '18 at 04:54
  • Try again. This is the way to do it. – Sid S Apr 15 '18 at 04:55
  • I changed the code now and got rid of the templates thing and also included the "Math.h" header in the "Math.cpp" source file like you said. It still throws the linking error however. – Spice Apr 15 '18 at 05:03
  • I didn't say that you should include `Math.h` in `Math.cpp`. I said you should inline the functions in `Math.h` and ditch `Math.cpp`. – Sid S Apr 15 '18 at 05:04
  • sorry my mistake, but, the thing is by declaring the functions inline, it would remove the entire point of trying to link a static library. – Spice Apr 15 '18 at 05:06
  • There is not much point in putting one-liners in a static library. If you're making one-line template functions, even less so - it only complicates things. – Sid S Apr 15 '18 at 05:10
  • Yes, I know. I am completely aware that this is a bit overkill for such a small file. I am just trying to come up with a simple example for stack overflow. My actual intention is to link the glfw3.a (found [here](http://www.glfw.org/download.html)) static library with my project but it throws this same error. I came up with this example as a small test to see if the problem lies with glfw or with something else... – Spice Apr 15 '18 at 05:13