LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2003, 07:05 PM   #1
ukpete
LQ Newbie
 
Registered: May 2003
Posts: 2

Rep: Reputation: 0
c++ within c...


Hi All,

I've run into a problem and wondered if anyone could help ? I've been programming c on linux for a while now and wanted to try a little c++. Rather than do it as standalone I wanted to call c++ routines from within some existing c code. It seems that can I call the c++ functions as long as they are basically 'c' code. For example:

stuff.cc:

extern "C" {
void startingfn (void) {
printf("hello\n") ;
}
}

and...

stuff.h:

#ifndef _STARTING_H
#define _STARTING_H

extern void startingfn(void) ;

#endif

I can compile this (using gcc - defined in the makefile as used for all the c files) and call the function from my c code (with the stuff.h file included). All's fine, but if I do this:

stuff.cc:

#include <iostream.h>
extern "C" {
void startingfn (void) {
cout << "hello" << endl ;
}
}

it fails with the following message:

/.../libstarting.a(rest.o): In function `startingfn(void)':stuff.o(.text+0x4): undefined reference to `endl(ostream &)'
stuff.o(.text+0xe): undefined reference to `cout'
stuff.o(.text+0x13): undefined reference to `ostream:perator<<(char const *)'
stuff.o(.text+0x1e): undefined reference to `ostream:perator<<(ostream &(*)(ostream &))'
collect2: ld returned 1 exit status
make: *** [gbna] Error 1

I've tried a few variations to work around it but to no avail. I can compile the .cc file with g++ to create a working executable, but I thought this was unneccessary ?

Does the problem lie with the gcc compiler ?

Hoping for an easy solution .
 
Old 06-01-2003, 04:04 AM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Sure, just compile all your code as C++ code, including the pure C (change its filename extension to .cc or .cpp).

Alex
 
Old 06-01-2003, 01:49 PM   #3
ukpete
LQ Newbie
 
Registered: May 2003
Posts: 2

Original Poster
Rep: Reputation: 0
So, given that I have a large number of exisitng 'c' code files, and I want to add a single 'c++' file into the mix - I'm supposed to rename all my 'c' files and recompile everything ?

Surely there's a better way to do this ?

I wouldn't expect this path to be necessary, for pure business reasons, large amounts of old code sitting around already written and working in 'c' - introduce some 'c++' and everything has to be moved around in this way ? Sounds like an odd way of doing it...?
 
Old 06-01-2003, 01:55 PM   #4
iceman47
Senior Member
 
Registered: Oct 2002
Location: Belgium
Distribution: Debian, Free/OpenBSD
Posts: 1,123

Rep: Reputation: 47
no, you don't have to rename them, just compile it with a c++ compiler
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration