help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: newbie g++ / gcc makefile???


From: Martin Jørgensen
Subject: Re: newbie g++ / gcc makefile???
Date: Sun, 29 Oct 2006 15:39:05 +0100
User-agent: Mozilla Thunderbird 1.0.6 (Macintosh/20050716)

Ulrich Eckhardt wrote:
Martin Jørgensen wrote:

I just made my first C++/C combined project and it works on windows. Now I want it to work on linux. So I have changed main.c to main.cpp


I don't think you mean that, but anyways: there is no reason to name things
differently in order to compile them under Linux.

Oh, I think this might be a misunderstanding because also on windows, the main.c is now called main.cpp - if that was what you were referring to...... I read that it was necessary to have the main() function in C++, in order to mix those languages...

and also have a new C++-function in the file: output_energy.cpp. The rest are C-functions.

How should the make-file be modified? I know that main must be processed by a C++-compiler so I changed CC = gcc to CC = g++ in the makefile below


Nope, that's wrong:
1. You don't set CC in the makefile, 'make' knows quite well the name of
the C compiler and this disallows overriding by the user (e.g. to select a
specific version).

Thanks.

2. The C++ compiler is CXX and also known to 'make'


.c.o:
        $(RM) $@
        @ #
        $(CC) -c $(CFLAGS) $(SRC)/$*.c


# similarly for C++
.cpp.o:
    $(CXX) $(CXXFLAGS) -c $(SRC)/$*.c

However, I think the placeholder for the input file is $< (i.e. the one
currently being compiled), see the manual.

The "gnu make manual" called make.pdf from www.gnu.org, right? I downloaded it...

    PROTO_DEFINES = -DFUNCPROTO=15 -DNARROWPROTO
STD_DEFINES = -Dlinux -D__i386__ -D_POSIX_C_SOURCE=199309L

                       ^^^^^^^^^^^^^^^^^^^
Don't do that either, it make the makefile non-portable and those (or
similar ones) should be defined by the compiler (assuming you are using it
under Linux and on an x86 architecture).

I just stole it from somewhere, but I don't know what it means... Now I deleted it completely.

CFLAGS = $(PROTO_DEFINES) $(STD_DEFINES) -g -W -Wall -ansi


The -W is deprecated and now called -Wextra, IIRC.

I think you're right...

simulate:  $(OBJ)
        $(RM) $@
        $(CC) -o $@ -g -W -Wall -ansi -pedantic -Wformat-nonliteral[...]


Wait a second, are you repeating all the warning settings here? Use CFLAGS

Erhm.... Perhaps I do... I wasn't really sure what this makefile did in the first place :-)

instead. Anyhow, this is the linker stage, and things like '-ansi' only
affect compilation so this doesn't make sense here anyway. Also, if you
are linking a C++ program, you need to use CXX for that.

Hmm. Okay. So in the linker stage I need something else than CFLAGS. And since main() is a C++ function I need CXX to do the linking of all .o files, I guess...

-snip (error messages) -
....
....

This is a result of compiling C code with a C++ compiler. C allows
converting a void pointer to any other pointer without an explicit cast,
C++ with its stricter type safety doesn't. Use a C compiler or fix the
code. Or, in general, don't blindly use a C++ compiler, there is no reason
for it.

Got it.

What do I do? I think I need to tell my makefile that most of the files should be compiled by a C-compiler (gcc?) and main.cpp and the other .cpp files should be compiled with g++?


Right, I hope I showed you how to separate those.

Yep, I think you helped me a lot... I think I'm very close to being able to compile my newly transformed C++ (originally a C)-program:


--- new makefile ---
    RM = rm -f
        SRC   = ./
        SYS_LIBRARIES = -lm

CFLAGS = -g -c -Wextra -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings -Wno-conversion -ffloat-store -O0

        CXXFLAGS = -g -c -O0 -Wall


# output program (executable file name)
PGM = simulate

# files that must be made by C-compiler (CC)
OBJ_C = \
        array_functions.o \
        band_solve.o \
        check_errors.o \
        import_data.o \
        memory_functions.o \
        mesh_functions.o \
        polymesh.o \
        porosities.o \
        read_materials.o \
        result_output.o

# files that must be made by C++-compiler (CXX)
OBJ_CPP = \
        main.o \
        output_energy.o

simulate: $(OBJ_C) $(OBJ_CPP)
        $(RM) $@
$(CC) -o $@ $(CFLAGS) $(OBJ_C) $(LIBPATH) $(SYS_LIBRARIES) Solverlib.a $(CXX) -o $@ $(CXXFLAGS) $(OBJ_CPP) $(LIBPATH) $(SYS_LIBRARIES) Solverlib.a

        etags `find -iname "*.c"` `find -iname "*.cpp"` `find -iname "*.h"`


clean::
$(RM) simulate *.CKP *.ln *.BAK *.bak *.o *.u a.out core errs ,* *~ .emacs_* tags TAGS make.log MakeOut "#"*

----

So I cleaned up a lot... Was also tired of not understanding what was in it... When I try to compile my C++ program (using many C-functions) I get:

---- output ---
....
....
....
cc -g -c -Wextra -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings -Wno-conversion -ffloat-store -O0 -c -o result_output.o result_output.c
g++ -g -c -O0 -Wall   -c -o main.o main.cpp
g++ -g -c -O0 -Wall   -c -o output_energy.o output_energy.cpp
rm -f simulate
cc -o simulate -g -c -Wextra -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings -Wno-conversion -ffloat-store -O0 array_functions.o band_solve.o check_errors.o import_data.o memory_functions.o mesh_functions.o polymesh.o porosities.o read_materials.o result_output.o -lm Solverlib.a
cc: array_functions.o: linker input file unused because linking not done
cc: band_solve.o: linker input file unused because linking not done
cc: check_errors.o: linker input file unused because linking not done
..... etc......
... (more lines with linking not done)
...
----

So, what is the (hopefully finally?) push in the right direction I need now? Comments/suggestions are appreciated. I also would like to have the g++ warning level turned up to the "ultimate maximum level", if possible.


Best regards
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]