views:

1927

answers:

1

Ok so I'm having trouble with my linking of files. Basically, my program works: the main program, is gen1 gen1 - recieves input sends to str2value for processing, outputs resaults str2value, brakes input into tokens using "tokenizer" deterines what sort of processing to do to each token, and passes them off to str2num, or str2cmd. then returns a array of the resualts str2num - does some processing str2cmd - ditto

author.py - a python script that generates str2cmd.c and str2cmd.h from a header cmdTable.h.

I'm pretty sure iu have my includeds right, I've checked a couple of times. I've also chect that there is no conditions #ifndef wrong in the headers.

BTW Yes i am aware that gcc is not a compiler, it's a program that passes of a file to the appropriate compile, I know this.

Here is my Makefile:

#CPP = g++ -lserial
CPP = g++ -DTESTMODE
C= gcc
DEFINES = LURC
CFLAGS = -Wall -fshort-enums -D$(DEFINES)
PROJECTFILES = gen1.cpp str2value.o

STR2VALUEFILES = str2value.cpp str2cmd.o str2num.o tokenizer.o str2value.h

gen1 : $(PROJECTFILES)
        $(CPP) $(CFLAGS) -o gen1 $(PROJECTFILES)



str2value.o : $(STR2VALUEFILES)
#       echo "str2value"
        $(CPP) $(CFLAGS) -c $(STR2VALUEFILES)

str2num.o: str2num.cpp  str2value.h str2num.hpp
         $(C) $(CFLAGS) -c $^


tokenizer.o: tokenizer.cpp tokenizer.hpp
        $(CPP) $(CFLAGS) -c $^

str2cmd.o : authorCMDs.py cmdTable.h
        python authorCMDs.py cmdTable.h str2cmd #this uses the gcc -E cmdTable.h -DLURC
        $(C) $(CFLAGS) -c str2cmd.c str2cmd.h

#should add a thing that checks str2cmd.h/.c has not been modified by hand
# i could actually compile them to an doxygen them then delete the source files



.PHONEY: clean
clean:
        rm *.o

.PHONEY: all
all:
        clear
        make clean
        make

Here is the output I recieve from make all

make clean
make[1]: Entering directory `/home/frames/LURC/gen1/gen1Source'
rm *.o
make[1]: Leaving directory `/home/frames/LURC/gen1/gen1Source'
make
make[1]: Entering directory `/home/frames/LURC/gen1/gen1Source'
python authorCMDs.py cmdTable.h str2cmd #this uses the gcc -E cmdTable.h -DLURC
str2cmd.c and str2cmd.h, generated from cmdTable.h

gcc  -Wall -fshort-enums -DLURC -c str2cmd.c str2cmd.h
gcc  -Wall -fshort-enums -DLURC -c str2num.cpp str2value.h str2num.hpp
g++ -DTESTMODE -Wall -fshort-enums -DLURC -c tokenizer.cpp tokenizer.hpp
g++ -DTESTMODE -Wall -fshort-enums -DLURC -c str2value.cpp str2cmd.o str2num.o tokenizer.o str2value.h
g++: str2cmd.o: linker input file unused because linking not done
g++: str2num.o: linker input file unused because linking not done
g++: tokenizer.o: linker input file unused because linking not done
g++ -DTESTMODE -Wall -fshort-enums -DLURC -o gen1 gen1.cpp str2value.o
str2value.o: In function `getValue(char*)':
str2value.cpp:(.text+0xbd): undefined reference to `str2cmd(char*)'
str2value.cpp:(.text+0x102): undefined reference to `str2num(char*)'
str2value.o: In function `getAllValues(char*)':
str2value.cpp:(.text+0x164): undefined reference to `tokenizer::tokenizer(char*)'
str2value.cpp:(.text+0x177): undefined reference to `tokenizer::getNumTokens(char const*)'
str2value.cpp:(.text+0x1a9): undefined reference to `tokenizer::getNextToken(char const*)'
str2value.cpp:(.text+0x1e9): undefined reference to `tokenizer::getNumTokens(char const*)'
str2value.cpp:(.text+0x201): undefined reference to `tokenizer::~tokenizer()'
str2value.cpp:(.text+0x25b): undefined reference to `tokenizer::~tokenizer()'
collect2: ld returned 1 exit status
make[1]: *** [gen1] Error 1
make[1]: Leaving directory `/home/frames/LURC/gen1/gen1Source'
make: *** [all] Error 2

any suggestions about what this is about? STR2VALUESFILES has all the object files I need, to define the missing functions

+2  A: 

I think you are confused about how compiler puts things together. When you use -c flag, i.e. no linking is done, the input is C++ code, and the output is object code. The .o files thus don't mix with -c, and compiler warns you about that. Symbols from object file are not moved to other object files like that.

All object files should be on the final linker invocation, which is not the case here, so linker (called via g++ front-end) complains about missing symbols.

Here's a small example (calling g++ explicitly for clarity):

PROG ?= myprog
OBJS = worker.o main.o

all: $(PROG)

.cpp.o:
        g++ -Wall -pedantic -ggdb -O2 -c -o $@ $<

$(PROG): $(OBJS)
        g++ -Wall -pedantic -ggdb -O2 -o $@ $(OBJS)

There's also makedepend utility that comes with X11 - helps a lot with source code dependencies. You might also want to look at the -M gcc option for building make rules.

Nikolai N Fetissov
how can i build an object file, out of other object files, then?gen1 has no direct dependancies with str2num str2cmd or tokenizer
Oxinabox
You compile each `.cpp` file individually, then link all resulting `.o` files together. I added a simple `make` example.
Nikolai N Fetissov
Forgot to mention - manage dependencies via header files, not object files.
Nikolai N Fetissov
I'm having trouble followign your example.I've never seen $@, @< or you use of .cpp.o
Oxinabox
`.cpp.o` rule tells `make` how to build a file with `.o` suffix out of corresponding file with `.cpp` suffix (say, how to produce `main.o` out of `main.cpp`) The `$<` is the "input" of the rule, `$@` is the "output" (again, `$<` would be `main.cpp`, `$@` would be `main.o`) These are *builtin variables* of `make`.
Nikolai N Fetissov
I changed the line which was previosly not working - compiling objects into an object isn't allowed.into consolidating obects into a library.It works, thanks.
Oxinabox