help-gplusplus
[Top][All Lists]
Advanced

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

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


From: Ulrich Eckhardt
Subject: Re: newbie g++ / gcc makefile???
Date: Sun, 29 Oct 2006 09:36:49 +0100
User-agent: KNode/0.10.4

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.

> 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).
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.

>      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).

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

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

> 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
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.


> cc1plus: warning: command line option "-Wstrict-prototypes" is valid for 
> C/ObjC but not for C++
> cc1plus: warning: command line option "-Wmissing-declarations" is valid 
> for C/ObjC but not for C++
> cc1plus: warning: command line option "-Wnested-externs" is valid for 
> C/ObjC but not for C++

These should be clear, C++ never allowed implicit declarations and
therefore a switch that disallows them makes no sense, other than for C.

> .//porosities.c: In function 'void p_factor_cell_results(double**, int, 
> int, double*, double*, double*, double*, double**, unsigned int)':
> .//porosities.c:65: error: invalid conversion from 'void*'
> to 'tablevalues*' 

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.

> 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.

happy hacking

Uli


-- 
http://gcc.gnu.org/faq.html
http://parashift.com/c++-faq-lite/



reply via email to

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