0

Edit: ***Sorry I realize there must have been some misunderstanding when posting this question, I want to specify that I DO realize this question was asked a few times, none of the answers work for me however***

I've been trying to link a simple static library test 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:

#include "Math.h"

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 `max(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?

I also want to re-mention that this is just a test library and I am fully aware that it might be a bit overkill to compile an entire static library from such a simple program. I am simply trying to figure out how to link libraries to my c++ projects...

Spice
  • 216
  • 1
  • 3
  • 12
  • 3
    Can't reproduce. Is it **your actual code**? In error message there is `int max(int, int)`, but you don't have templates in your code. – HolyBlackCat Apr 15 '18 at 08:33
  • 1
    I can't reproduce either. – BobMorane Apr 15 '18 at 14:14
  • @HolyBlackCat, I changed my code previously and forgot to change its respective error. The linker still, however, threw the same error with undefined reference to `max(int, int)`. This is the **Exact** code I am using... – Spice Apr 15 '18 at 17:06
  • @BobMorane, If it is as you say, and you guys can't reproduce this error, then would you say it's a problem with the linker all together? Maybe should I reinstall MinGW? Afterall, I am using a pretty old versioin of MinGW. – Spice Apr 15 '18 at 17:06
  • @Xamser7 I compiled and linked your program using `g++` and `ar` with your exact code and shell commands on Linux and have had no problem. It links and runs fine. I'm not sure about MinGW, but in your position I would certainly start trying to reproduce it with different compilers/linkers/environments and try to figure out what's going on. – BobMorane Apr 15 '18 at 18:12
  • @BobMorane I am using windows 7 and windows 10 to do all of this, would that be the problem? – Spice Apr 15 '18 at 18:26
  • @Xamser7 I'm not sure. Have you tried doing the same thing with the Microsoft compiler/linker? – BobMorane Apr 15 '18 at 19:11
  • 1
    What's output of `g++ --version`? It works on my MinGW-w64 (GCC 7.3.0), so you can download it if you want. – HolyBlackCat Apr 15 '18 at 19:24
  • @HolyBlackCat I am using MinGW-w64 also, but mine is (GCC 6.3.0). Do you know where I can get the newest version? Because [mingw.org](http://mingw.org/) just redirects me to the respective [sourceforge](https://sourceforge.net/projects/mingw/files/) link, which only provides me with 6.3.0 for some reason. – Spice Apr 15 '18 at 22:06
  • There is big difference between MinGW-w64 and plain MinGW. [Here's a link for you](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe/download). Also it's generally better to provide full output of `g++ --version` because it often says more than just the version number. – HolyBlackCat Apr 15 '18 at 22:16

0 Answers0