Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-07-2006, 06:04 AM   #1
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?

Old 04-07-2006, 08:28 AM   #2
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:
#ifdef __cplusplus
#define new _new
#include <linux/lvm.h>
#undef new

Last edited by Flesym; 04-07-2006 at 08:30 AM.
Old 04-11-2006, 07:20 AM   #3
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, 09:45 AM   #4
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:
extern "C" {
#include "c-source-file.h"
This project demonstrates:
/* cpp-code.cpp */

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

   int *ptr = new int;


   delete ptr;
/* c-code.h */

#ifndef C_CODE_H
#define C_CODE_H

void c_func();

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

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

   int new = 4711;

   printf("%i\n", new);
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) $<

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


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 09:36 PM
mixing * ports error in apache + ssl xushi Slackware 1 09-21-2004 10:03 AM
compilation error - code 2 jhon Programming 2 08-31-2004 01:54 AM
Major compilation errors in emacs while compiling c code dualcyclone Linux - Software 0 03-26-2004 01:31 PM
Mixing Windows & Linux combatgirl Mandriva 2 12-12-2003 04:58 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 PM.

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