Reputation: 6980
I have a file Cache.cpp that has a corresponding header file Cache.h and another NetFunctions.cpp that has a corresponding header file NetFunction.h.
I have a makefile that looks like this
all: net cache
g++ main.cpp ../obj/NetFunctions.o ../obj/Cache.o -o ../bin/main
net: NetFunctions.cpp
g++ -c NetFunctions.cpp -o ../obj/NetFunctions.o
cache: Cache.cpp net
g++ -c Cache.cpp ../obj/NetFunctions.o -o ../obj/Cache.o
Now NetFunctions.cpp has a function getNFHTML(string) defined there, which is used in Cache.cpp. I have checked the header files and they look fine with all the functions, declared there and header files properly included.
However, when I make, I get the following linker error
../obj/Cache.o: In function `Cache::getHTML(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
Cache.cpp:(.text+0x19ee): undefined reference to `getNFHTML(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
make: *** [test] Error 1***
Can someone help me with this? What is the problem?
I have also referred to this post C++ Linking error, but it was of no help to me.
Here is the code: NetFunctions.h
#ifndef NET
#define NET 1
#include "commons.h"
bool serv_bind(struct addrinfo **servinfo, struct addrinfo **p, int *sockfd, int *yes);
void* get_in_addr(struct sockaddr *sa);
string getNFHTML(string website);
string saveHeaders(string);
//bool getNFHTML(string request, string last_modified, string *response);
extern bool useCache;
#endif
NetFunctions.cpp
#include "NetFunctions.h"
string getNFHTML(string request)
{
// a lot of code
}
Cache.cpp
#include "NetFunctions.h"
#include "Cache.h"
string Cache::getHTML(string request)
{
//some code
string response = getNFHTML(request);
//some code
}
I have stripped the files as they contained several hundred lines
Upvotes: 0
Views: 2027
Reputation: 409136
This has nothing to do with the problem, but I put it in an answer so it will be better formated.
Your makefile doesn't handle dependencies very well. Try this one instead:
.PHONY: all
all: ../bin/main
../bin/main: main.cpp ../obj/NetFunctions.o ../obj/Cache.o
g++ $^ -o $@
../obj/NetFunctions.o: NetFunctions.cpp
g++ -c $< -o $@
../obj/Cache.o: Cache.cpp
g++ -c $< -o $@
For the ../bin/main
target, the variable $^
means to take all prerequisites, and $@
is the target of the rule. This means you can add as many object files as you like, and all will be linked.
For the compilation of the source to object files, the $<
variable is the first prerequisite. This will also make it easier to copy/paste the targets if you add more files.
The rules also make sure that when you build ../bin/main
all files it depends on will be built properly.
The first target, all
, will be the default if you run make
without specifying a target. It has been marked as a phony target, because it doesn't produce a file called all
.
Note that I'm basing this on GNU make, which is standard in almost all Linux distributions, and in Cygwin/MingW. If you use BSD make the variables might change.
Upvotes: 1
Reputation: 213375
While your Makefile
is pretty broken, it's pretty clear that if what you are saying:
NetFunctions.cpp has a function getNFHTML(string) defined there
was true, then it would have worked. Therefore it must be that you are lying to us. Show the code for getNFHTML
in NetFunctions.cpp
, or at least the output from:
nm ../obj/NetFunctions.o | grep getNFHTML
Update:
Ok, what you told us is true. However, it appears that the target that is failing to link is called test
, and there is no such target in your makefile.
I am guessing that that target is being linked without the required NetFunctions.o
on the link line.
In any case, please update your question with actual link line that is failing.
Upvotes: 0
Reputation: 212929
Change:
cache: Cache.cpp net
g++ -c Cache.cpp ../obj/NetFunctions.o -o ../obj/Cache.o
to:
cache: Cache.cpp net
g++ -c Cache.cpp -o ../obj/Cache.o
Better yet, get rid of the dummy targets and turn on compiler warnings:
../bin/main: ../obj/NetFunctions.o ../obj/Cache.o
g++ -Wall main.cpp ../obj/NetFunctions.o ../obj/Cache.o -o $@
../obj/NetFunctions.o: NetFunctions.cpp
g++ -Wall -c NetFunctions.cpp -o $@
../obj/Cache.o: Cache.cpp
g++ -Wall -c Cache.cpp -o $@
Upvotes: 0