The Makefile should look in specific folders

Hi,

I am using a Meinberg GPS Card and trying to compile a program which should interact with the Card. The GPS Card is properly installed, I can use all the sample programs.
My programm basically is a .cc file.
When I try to compile I get an error message saying I "undefined reference". The so called undefined refernece is definde in a .c file which is included in the header file of my .cc program.
So the Meinberg Support told me I had to include the .c files too in my Makefile.

A friend of mine wrote me then this piece of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# specific names for this package
TARGET = v265_h
CSOURCES = $(wildcard *.c) 
CCSOURCES = $(wildcard *.cc)
HEADERS = $(wildcard *.h) 
CCOBJECTS = $(CCSOURCES:.cc=.o)
COBJECTS = $(CSOURCES:.c=.o)
OBJECTS = $(CCOBJECTS)
OBJECTS += $(COBJECTS)
PATH1 = ~/VME_messungen/

# include options for this package
INCFLAGS = -I /local/home/gerda/GPS_Karte/mbgtools-lx-3.0.0/mbglib/common -I /local/home/gerda/GPS_Karte/mbgtools-lx-3.0.0/mbglib/linux

CXX   = g++
LD              = g++
#FFLAGS          += -Wall
#FLDFLAGS        += -lstdc++ -shared
CXXFLAGS        += -g -O -W -Wall -Wno-deprecated

# default target list
all: init main

init:
# $(SHELL source ./pc76.sh)

$(CCOBJECTS): %.o: %.cc
  $(CXX) $(CXXFLAGS) -c $<

$(COBJECTS): %.o: %.c
  $(CXX) $(CXXFLAGS) -c $<

main: $(COBJECTS) $(CCOBJECT)
	$(CXX) -L. -lCAENVME -lncurses -o $(TARGET) $(CXXFLAGS) $(EXTRALIBS) $(OBJECTS) /local/scratch0/astro/VME_lib/console.o


But I still get the same error. Then I got the tip that I should try and move all the reference GPS Card fils to the directory where my Makefile is and comile again.
I did and I got a different error regarding another library file from the card. I began to manually go through the error messages and set the paths of the files absolute and it worked but I will never finish this way^^

How do I have to change the code of the Makefile if I want it to check everywhere?

I hope you guys can help.

Thank you
.c files should not be included in header files. The .c file should have its own header declaring its public contents. That header file should be included in your .cc file.
So you are saying the Makefile is correct?

I will translate the exact answer from the support for you:
"You have to include the corresponding .c files (to the Header files I included in my .cc program) in the Makefile because We don't deliver libs as precompiled shared libraries at the moment."

And you are saying this is not possible? Or did I get it wrong?
I see, you worded it differently.

What they mean is that they supplied you with some .h files and some .c files. They all need to be in the same folder as your .cc file.
Usually for C libraries you get from external vendors, they will supply a .h file so you know what are the function and arguments to call BUT the implementation come in the form of .a and .so suffixes to indicate static or dynamic libraries.

Then in your own program you #include the vendor .h file and then during linking you link their provided libraries in order to get your program to run 'talking' to their libraries.

This vendor is strange, they actually give you the source code implementation of their library hmmm... pretty generous for a vendor isn't it ? :)
Typically this is what you need to do:

1. #include header files
2. link with library/object(s)

The first part is how your application knows what to expect out of the library. The second part is what you need to do in your Makefile.

To make the problem easier to diagnose, post more specific information. We can't say the Makefile is fine without seeing what targets are available. Pasting the errors verbatim would be very helpful, as well.
Last edited on
Ok thanks for your replies:

When I type make now, it says "undefined reference to ....." This specific command is defined in a .c file which header is included in my .cc program.

Now, when I copy all the header files and the corresponding .c files to the directory where my .cc file is and run make again like Galik suggested, I get an error from another .c file which points to his corresponding .c file saying: "no such file or directory" for the corresponding .c file which is definitely in the same diretory. When I edit the path to the .c file in the .h file to /home/hilikus/.../file.c it finds the file but brings another errors. Now I can edit the paths manually into the .c and .h files whenever he brings that error and it seems to work but I did that this morning for over two hours and wasn't done. There are quiet a lot of .c and .h files it seems^^

Do you now have all information you need?
In your Makefile you need to put the paths to all your .h files on this line:
1
2
INCFLAGS = -I /local/home/gerda/GPS_Karte/mbgtools-lx-3.0.0/mbglib/common \
	 -I /local/home/gerda/GPS_Karte/mbgtools-lx-3.0.0/mbglib/linux

The -I flag to the compiler tells it what folder to look for .h files.

The other thing you could try is modifying line 31 of the Makefile to say this:
 
 gcc $(CXXFLAGS) -c $<

It might be failing to find something because it is compiling the C files using the C++ compiler. But that also might be what they intended so its worth a shot but it might make things worse too ;o)

Without being able to see the whole setup its difficult to diagnose.
Last edited on
When I type make now, it says "undefined reference to ....." This specific command is defined in a .c file which header is included in my .cc program.


Does that .c file compile into a .o? It would need linked in before your application can find the symbols that only it contains.
Last edited on
The -I flag to the compiler tells it what folder to look for .h files.

It may also be worthwhile to note that this affects the include path when using #include with <>'s, not ""'s.
Also do try to diagnose you could modify the last few lines of the Makefile like this
1
2
3
main: $(COBJECTS) $(CCOBJECT)
	echo objects: $(OBJECTS) 
	$(CXX) -L. -lCAENVME -lncurses -o $(TARGET) $(CXXFLAGS) $(EXTRALIBS) $(OBJECTS) /local/scratch0/astro/VME_lib/console.o

That should give you a list of .o files. There should be one for every .c and every .cc file you have.
Topic archived. No new replies allowed.