I have been using a makefile that I inherited from the internet for long enough! It is time that I learn to compile my own projects once and for all. So I’ve read some tutorials and thrown together a mock-up. It doesn’t work, and I am at my wits end now (which is usually when I come to YOU).
As usual, thank you all in advance for being so knowledgeable.
lib.h
#ifndef LIB_H
#define LIB_H
/** returns the integer zero (0), because the
* 0 key on my keyboard is broken.
* @pre my keyboard is broken
* @post nope, still broken
* @returns 0 */
int zero();
#endif
lib.cpp
#include "lib.h"
int zero() {
return 0;
}
project.cpp
#include "lib.h"
int main () {
int i = zero();
return 0;
}
makefile
(the arrows are tab characters)
project: lib.o project.o
-->g++ lib.o project.o -o project
project.o: project.cpp lib.h
-->g++ -c project.cpp
lib.o: lib.cpp lib.h
-->g++ -c lib.cpp
When, from the command line, I run
$ make
I get the following error:
g++ project.cpp
/tmp/ccWCSqSx.o: In function 'main':
project.cpp:(.text+0x9): undefined reference to 'zero()'
collect2: ld returned 1 exit status
make: *** [project.o] Error 1
This is clearly a case of me having missed some crucial piece of information somewhere along the lines. I’ve googled this a bunch, but most of the results have been people with real problems, and I haven’t been able to learn what’s missing. Looking forward to being tremendously embarrassed by the answer,
Thanks!
The error message gives the command line:
Note the lack of -c. That’s apparently not in your makefile, in spite of what you think.