1

I needed to include a few c headers ( non standard header files ) in my C++ code to be compiled by gcc. The C header (foo.h) has support for :

#ifdef __cplusplus 
extern "C" {  
#endif 

and similarly at the end for }. The c++ code has the include "foo.h" I believe I should be able to just include the header (foo.h) and create instances of structs defined in the .h file.

I am not able to compile the source code. It seems like the compiler looks at the c code as if it were c++ code. I see error such as

error: expected constructor, destructor or type conversion before "("

Did I do something wrong ? I took advise from : http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

What else do i need to do, to tell the c++ compiler "expect and compile as c code" ?

4
  • When pasting code, select it and press the button with the binary digits. That will indent the lines and produce formatted code. Consider also adding more information on what headers you are including, how and what the exact error is (what is before the ')' in the error message) Commented May 26, 2010 at 14:19
  • 4
    Welcome to SO! You'll need to show more since usually those guards are sufficient. Show the line number that it's complaining about and maybe get a smaller .cpp file to show off the problem. Commented May 26, 2010 at 14:20
  • 2
    It's possible that the C header you are using uses as an identifier a C++ keyword that was not a keyword in C. However if the author put in the extern "C" block, he was probably smart enough to avoid that. This question needs to show more of the relevant code, or all you'll get is guesses. Commented May 26, 2010 at 14:23
  • 8
    The extern "C" construct does not tell the compiler to compile your code as C, it tells it to give it C linkage. You need to post the actual code that gives the error. Commented May 26, 2010 at 14:24

1 Answer 1

1

Thanks for all your replies !

My problem was i had not included all the needed header files / in the right order . I am all set.

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.