You are not logged in.
Hello.
I am working on some code snippets that I finally want to put together.
There are three relevant files that cause the compiling error:
mpdscrob.cpp
options.h
librc.h
Here are the include-Directives of each file:
mpdscrob.cpp
#include <iostream>
#include <unistd.h> /* sleep() */
#include <mysql/mysql.h>
#include "mpd.h"
#include "options.h"
options.h
#include <string>
#include "librc.h"
librc.h
#include <string>
#include <fstream>
#ifdef DEBUG
#include <iostream>
#endif
I build the program via this makefile:
makefile
VERSION = 0.1
CC = /usr/bin/g++
CFG = `mysql_config --cflags --libs`
all: options.o mpd.o mpdscrob.cpp
$(CC) mpdscrob.cpp mpd.o options.o -o mpdscrob $(CFG)
options.o: librc.o options.h options.cpp
$(CC) options.cpp librc.o -c -o options.o
mpd.o: mpd.h mpd.cpp
$(CC) mpd.cpp -c -o mpd.o
librc.o: librc.h librc.cpp
$(CC) librc.cpp -c -o librc.o
clean:
rm mpd.o
rm mpdscrob
rm librc.o
rm options.o
As you see mpdscrob needs options.o to compile (mpd.o doesn't cause any trouble) and options.o needs librc.o to compile.
I can compile options.o without trouble but when I want to compile all (say mpdscrob), g++ says:
/usr/bin/g++ mpdscrob.cpp mpd.o options.o -o mpdscrob `mysql_config --cflags --libs`
options.o: In function `Options::read_from_file(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
options.cpp:(.text+0x14): undefined reference to `Rc_File::Rc_File()'
[...]
collect2: ld gab 1 als Ende-Status zurück
make: *** [all] Fehler 1
And many other lines that complain about missing references to librc.h which contains the class Rc_File, etc.
Well... I don't know why g++ need to know about these references because they're already included into options.o but, well. I just added this to mpdscrob.cpp:
#include "librc.h"
But then g++ is complaining about redefinition:
/usr/bin/g++ mpdscrob.cpp mpd.o options.o -o mpdscrob `mysql_config --cflags --libs`
librc.h:10: Fehler: Redefinition von »class Rc_File«
librc.h:10: Fehler: vorherige Definition von »class Rc_File«
make: *** [all] Fehler 1
I am quite new to compiling and don't know how to solve this the proper way... well... no I don't know how to solve this at all.
Thankful for every suggestion.
harlekin (:
Hail to the thief!
Offline
First, header files must be written between defines, to prevent double inclusion
#ifndef MY_HEADER
#define MY_HEADER
// ...
#endif
Whenever you use that functions defined in the header, you include it. You will not have to worry about double definitions again though.
It is good to compile each C/C++ file to obtain the object file and, afterwards, to link all the object files together.
When compiling an object file with -c, you
* don't have to tell the compiler which files it has to link (see your rule for options.o). You can do the linking at the end, for the executable
* don't have to specify the output file with -o, the output is automatically named after the source file
The rule making the executable should be named as the executable. "all" should depend on the executable rule.
If I were in your place, I would have my Makefile like this
VERSION = 0.1
CC = /usr/bin/g++
CFG = `mysql_config --cflags`
LFG = `mysql_config --libs`
all: mpdscrob
mpdscrob: options.o mpd.o mpdscrob.o
$(CC) $(LFG) mpdscrob.o mpd.o options.o -o mpdscrob
mpdscrob.o: mpdscrob.cpp
$(CC) $(CFG) mpdscrob.cpp -c
options.o: options.h options.cpp
$(CC) options.cpp -c
mpd.o: mpd.h mpd.cpp
$(CC) mpd.cpp -c
librc.o: librc.h librc.cpp
$(CC) librc.cpp -c
clean:
rm *.o mpdscrob || exit 0
I use "exit 0" because rm returns with exit_code!=0 when it doesn't find something to remove and make fails (I don't like that).
I don't know if this stuff solves your problems. It's the way I do it and it's pretty clean. I rarely have problems with this.
:: / my web presence
Offline
The problem is in *linkage*, not compilation. I.e. the needed libraries are not listed in g++ command.
Offline
And how do I tell where to find them?
Tried to add the header files to the g++ arguments:
mpdscrob: mpdscrob.o mpd.o options.o
$(CC) $(LFG) -o mpdscrob librc.h mpdscrob.o mpd.o options.o
But the error persists.
I overflew `man g++` but couldn't find an answer either.
Seems to be a general problem of understanding the process. :
The missing references it is complaining about are documented in librc.h which is in the same directory as all other files.
IceRAM: Thanks, I improved my makefile and some header files concerning your points. (:
Hail to the thief!
Offline
If you did what IceRAM said you should be getting different errors now.
Offline
<code>#include "librc.h"</code> - what library does it belong to? Look manual for the library, generally, you'll need to add -lrc or something like this to the g++ command line
Offline
codemac: No, the error is still the same. Why it should be anything differend?
drakosha: librc is written by myself. It is comprised of these files:
librc.h
librc.cpp
The file options.cpp needs this librc and as for that I included it in options.h.
If I finally want to link options.o and mpdscrob.o g++ (still) is complaining about missing references to librc:
/usr/bin/g++ `mysql_config --libs` -o mpdscrob librc.h mpdscrob.o mpd.o options.o
[...]
options.cpp:(.text+0x14): undefined reference to `Rc_File::Rc_File()'
options.cpp:(.text+0x29): undefined reference to `Rc_File::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
options.cpp:(.text+0x195): undefined reference to `Rc_File::read(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
[...]
I just did a change in the makefile and now it's working:
all: mpdscrob
mpdscrob: librc.o options.o mpd.o mpdscrob.o
$(CC) $(LFG) -o mpdscrob librc.o mpdscrob.o mpd.o options.o
I was not away of that I had to include librc.o as well because I thought it is already included into options.o because I compiled it like this:
options.o: options.h options.cpp
$(CC) -c -o options.o options.cpp
Well... I could have thought of it but thanks anyway. I guess my make file and my header files are a little smarter now and my programming style needs a lot of smartness. :
Thanks! (=
Hail to the thief!
Offline