LinuxQuestions.org
Register a domain and help support LQ
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 04-07-2006, 07:04 AM   #1
mghiya
LQ Newbie
 
Registered: Feb 2006
Posts: 10

Rep: Reputation: 0
Unhappy Compilation error while mixing C & C++ code.


Hello ,

I have been including the file linux/lvm.h in my cpp file but when i try to compile i get the following error:
/usr/include/linux/list.h:37: parse error before `new'
What I understand from this problem is that I am trying to include C code header in a cpp file and invoking the cpp compiler on it. As I understand, new being a keyword in C++ conflicts with the use of a variable with the same name in list.h which is included in lvm.h

Is there any workaround for this?

~Murtaza
 
Old 04-07-2006, 09:28 AM   #2
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
You can define the keyword 'new' as something else before including the file. But don't forget to undefine it afterwards! Something like the following sample should work:
Code:
#ifdef __cplusplus
#define new _new
#include <linux/lvm.h>
#undef new
#endif

Last edited by Flesym; 04-07-2006 at 09:30 AM.
 
Old 04-11-2006, 08:20 AM   #3
mghiya
LQ Newbie
 
Registered: Feb 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Thats what I am currently doing anyways. Any other way of doing it???
 
Old 04-11-2006, 10:45 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Well, you should not try to compile the C source file as C++ source file if it contains constructs that are invalid C++. Instead, compile it as C and when you include the C header in a C++ source file, do:
Code:
extern "C" {
#include "c-source-file.h"
}
This project demonstrates:
Code:
/* cpp-code.cpp */

extern "C" {
#include "c-code.h"
}

int
main()
{
   int *ptr = new int;

   c_func();

   delete ptr;
}
Code:
/* c-code.h */

#ifndef C_CODE_H
#define C_CODE_H

void c_func();

#endif /* #ifndef C_CODE_H */
Code:
/* c-code.c */

#include "c-code.h"
#include <stdio.h>

void
c_func()
{
   int new = 4711;

   printf("%i\n", new);
}
Code:
CC = gcc
CXX = g++
CFLAGS = -Wall -W -ansi -pedantic -g -O0 -c
CXXFLAGS = -Wall -W -ansi -pedantic -g -O0 -c
OBJECTS = c-code.o cpp-code.o
EXEC = runme.exe

all: $(OBJECTS)
	$(CXX) $^ -o $(EXEC)

c-code.o: c-code.c
	$(CC) $(CFLAGS) $<

cpp-code.o: cpp-code.cpp
	$(CXX) $(CXXFLAGS) $<

clean:
	rm -f $(OBJECTS) $(EXEC) *~ *.stackdump
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
image quality, cables, mixing old & new hardware cast55 General 3 09-19-2005 10:36 PM
mixing * ports error in apache + ssl xushi Slackware 1 09-21-2004 11:03 AM
compilation error - code 2 jhon Programming 2 08-31-2004 02:54 AM
Major compilation errors in emacs while compiling c code dualcyclone Linux - Software 0 03-26-2004 02:31 PM
Mixing Windows & Linux combatgirl Mandriva 2 12-12-2003 05:58 AM


All times are GMT -5. The time now is 06:00 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration