makefile for mix of C and C++ modules


 
Thread Tools Search this Thread
Top Forums Programming makefile for mix of C and C++ modules
# 1  
Old 09-01-2011
makefile for mix of C and C++ modules

I am trying to come up with a makefile where the target is linked with object files produced by C and C++ sources.

My setup is Ubuntu/gcc:
$ uname -a
Linux srvr1 2.6.24-24-server #1 SMP Fri Sep 18 17:24:10 UTC 2009 i686 GNU/Linux

gcc version 4.2.4

$ cat main.cpp
Code:
#include <iostream>
using namespace std;
extern "C" int  clnt(int);
int main(int argc, char *argv[])
{
  int   x = argv[1] ? atoi(argv[1]) : 100;
  int   y = 0;
        y = clnt(x);
        cout << y;
        return 0;
}

$ cat client.c
Code:
int clnt(int x)
{
  int   y = 0;
        y = (x < 0) ? x * x : x + x;
        return(y);
}

$ cat makefile
Code:
CFLAGS          = -g
CPPFLAGS        = -Wall -ansi -g
OBJS            = main.o client.o
tester: $(OBJS)
        $(CC) -o $@ $(OBJS)

$ make
Code:
g++  -Wall -ansi -g  -c -o main.o main.cpp
cc -g -Wall -ansi -g  -c -o client.o client.c
cc -o tester main.o client.o
main.o: In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/include/c++/4.2/bits/locale_facets.tcc:2562: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
/usr/include/c++/4.2/bits/locale_facets.tcc:2571: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
/usr/include/c++/4.2/bits/locale_facets.tcc:2573: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
/usr/include/c++/4.2/bits/locale_facets.tcc:2578: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
main.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.2/iostream:77: undefined reference to `std::ios_base::Init::Init()'
main.o: In function `__tcf_0':
/usr/include/c++/4.2/iostream:77: undefined reference to `std::ios_base::Init::~Init()'
main.o: In function `main':
/home/mgurfink/mydev/amq/cms/main.cpp:12: undefined reference to `std::cout'
/home/mgurfink/mydev/amq/cms/main.cpp:12: undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
main.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [tester] Error 1

Question #1: why CPPFLAGS are used for the compilation of client.c - even though it correctly uses 'cc', not 'g++' for C file?
Question #2: why it failed to use C++ related stuff when linking?

I do have a working makefile, where I explicitly say to use g++ and it works, see below:
Code:
CC = g++
CFLAGS          = -g
CPPFLAGS        = -Wall -ansi -g
OBJS            = main.o client.o
tester: $(OBJS)
        $(CC) -o $@ $(OBJS)
main.o: main.cpp
        $(CC) -c $<
client.o: client.c
        gcc -c $<

But I'd like to keep it simple, as the actual build has many more different modules and I'd hate to have to write a target for every single one of them.

One more piece of info - actual CPPFLAGS need to be different from CPFLAGS, as some that I need are not supported by C compiler, such as -Wno-non-virtual-dtor
Any suggestions?
# 2  
Old 09-01-2011
You shouldn't need any rules to make .o files -- leave them out. make will know to build something.o from something.c or something.cpp as long as something else needs something.o make will also use CFLAGS for .c and CXXFLAGS for .cpp by default.

For the rule building tester, use $(CXX), not $(CC). If you have even one C++ object among there it needs C++ linking.
# 3  
Old 09-02-2011
The $(CXX) did the trick, thanks! Linker is happy.
One remaining issue is that CPPFLAGS are being applied to the plain C compilation, see below:
$ make
Code:
g++  -Wall -ansi -g  -c -o main.o main.cpp
cc -g -Wall -ansi -g  -c -o client.o client.c
g++ -o tester main.o client.o

I'd like to see client.c compiled as follows:

Code:
 
cc -g -c client.o client.c

as my CFLAGS is set to -g only.

Is it possible to hint make to use CPPFLAGS for C++ and CFLAGS for C files?
# 4  
Old 09-02-2011
I don't know why it's doing that and can't see your computer from here Smilie Post what your Makefile looks like now please.
# 5  
Old 09-02-2011
Sorry, I forgot to show my current make file

Code:
CFLAGS          = -g
CPPFLAGS        = -Wall -ansi -g
OBJ1            = main.o client.o
tester: $(OBJ1) 
        $(CXX) -o $@ $(OBJ1)

$ ls *.c *.cpp
Code:
client.c  main.cpp

$ make
Code:
g++  -Wall -ansi -g  -c -o main.o main.cpp
cc -g -Wall -ansi -g  -c -o client.o client.c
g++ -o tester main.o client.o

# 6  
Old 09-02-2011
Try CXXFLAGS instead of CPPFLAGS.
# 7  
Old 09-02-2011
Code:
CXX      = g++
CXXFLAGS = -Wall -ansi -g
CC       = gcc
CCFLAGS  = -g
OBJS     = main.o client.o

tester : $(OBJS)
        $(CXX) -o $@ $(OBJS)

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

%.o : %.c
        $(CC) -c $(CCFLAGS) $<

These 2 Users Gave Thanks to fpmurphy For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Mix LDAP and LOCAL user on AIX

Hello, I'm currently trying to mix local and LDAP users on an AIX 7.1. I've triied many things. My LDAP Server in on a CentOS - OpenLDAP (which works fine with linux). I'm currently stuck on AIX at how to declare LDAP AND Local users. Here's what i did : /usr/sbin/mksecldap -c -h 'ldap03'... (15 Replies)
Discussion started by: AIX_user_324891
15 Replies

2. Shell Programming and Scripting

Makefile Mix for C and C++

Hi everyone, I'm new in this forum. I hope you could help me with this code. I'm trying to include in an ANSI C project, some (OpenCV) libraries made in C++. When I included those libraries, the compiler threw the following error: ardrone_testing_tool.c: (.text+0x1212) : undefined reference... (4 Replies)
Discussion started by: hfbrando
4 Replies

3. Shell Programming and Scripting

how to create custom modules in perl and how to import all modules with single command?

I have some custom functions which i want to use in perl Scripting all time. i want to How to create modules in perl and how to import them. Also if i create 15 modules and i want to > import all at once then how can i import? (0 Replies)
Discussion started by: Navrattan Bansa
0 Replies

4. Homework & Coursework Questions

Help with Simple Multi-Level Makefile (Extremely New at Makefile)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Basically, the prompt is make a makefile with various sub makefiles in their respective subdirectories. All code... (1 Reply)
Discussion started by: Tatl
1 Replies

5. Shell Programming and Scripting

How to mix the contents of 2 files into a new file?

Hello Everybody! My question is how can I mix for example file_a with file_b in the following method: After 2 lines of file_a put 2 lines from file_b to file_c. For example: file_a: 1 2 3 4 5 6 file_b: 11 22 (7 Replies)
Discussion started by: Levi85
7 Replies

6. Solaris

ZFS Pool Mix-up

Hi all I plan to install Solaris 10U6 on some SPARC server using ZFS as root pool, whereas I would like to keep the current setup done by VxVM: - 2 internal disks: c0t0d0 and c0t1d0 - bootable root-volume (mirrored, both disks) - 1 non-mirrored swap slice - 1 non-mirrored slices for Live... (1 Reply)
Discussion started by: blicki
1 Replies

7. UNIX for Advanced & Expert Users

Makefile problem - How to run module load in a Makefile

Hi, I'm trying to run the module load command in a Makefile and i'm getting the following error: make: module: command not found Why is this? Is there any way to run this command in a Makefile? NOTE: command - module load msjava/sunjdk/1.5.0 works fine outside of the Makefile (2 Replies)
Discussion started by: hernandinho
2 Replies

8. AIX

mix fc and tape devices

I have a problem with some partitions. In one of them I have this two fiber channel. lsdev -Cc adapter ent0 Available 03-08 10/100/1000 Base-TX PCI-X Adapter (14106902) fcs0 Available 04-08 FC Adapter fcs1 Available 05-08 FC Adapter ide0 Defined 07-08 ATA/IDE... (0 Replies)
Discussion started by: lo-lp-kl
0 Replies

9. Shell Programming and Scripting

How to grep mix of numbers and systemdate?

Hi Guys, i'm beginner in UNIX commands, need some help on this simple question: I need to make a shell script to move files to another directory, the criterias are : 1. the range of 4 last digit of the file name is 0100-0199 2. move all files that processed daily (let's say today is... (2 Replies)
Discussion started by: saltshaker
2 Replies
Login or Register to Ask a Question