Thread: Incuding C++ header files in C, VS 2010

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    5

    Question Incuding C++ header files in C, VS 2010

    I want to write a C code, say "test.c" call some C++ functions from it.I have a header file header.h which has function defined in it and a C++ file which has definitions of functions.I'm unable to figure out compilation commands and how to use extern command. Can someone clarify this?
    Thanks in advance,
    Neerad

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read this FAQ: How to mix C and C++
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    5
    Hey, thanks. I have gone through that already but I couldn't find any instructions for compiling the code

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, can you just compile your project as C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    5

    Arrow I've compiled but there is a problem

    Quote Originally Posted by neeraddevil View Post
    Hey, thanks. I have gone through that already but I couldn't find any instructions for compiling the code
    insert
    Code:
    #ifdef __cplusplus
    extern "C"
    {
    #endif
      void foo (void);
    #ifdef __cplusplus
    };
    #endif
    
    Then I implement foo() in .cpp file. I also include .h in .cpp
    and it worked. But the problem is I can't define any classes in header file after I do this. If the classes are in .cpp file, it works

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you need C++ classes to be defined in the header file in the first place?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    5

    Arrow

    Quote Originally Posted by laserlight View Post
    Do you need C++ classes to be defined in the header file in the first place?
    I use header file given by another program.
    I can define classes when we use c++ code right?
    Y can't we do that when we use extern?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by neeraddevil
    I can define classes when we use c++ code right?
    Y can't we do that when we use extern?
    Classes don't exist as a language feature in C. It is not a matter of linkage, which is what the use of extern is about. One does not simply define a class in a header and include it in a C source file and expect it to compile.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    5

    Thumbs up

    Quote Originally Posted by laserlight View Post
    Classes don't exist as a language feature in C. It is not a matter of linkage, which is what the use of extern is about. One does not simply define a class in a header and include it in a C source file and expect it to compile.
    Ok. Thanks for the info

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by laserlight View Post
    One does not simply define a class in a header and include it in a C source file and expect it to compile.
    I see what you did there, intentionally or not!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 7 RC, Visual Studio 2010 Beta, Office 2010 beta, anyone in?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-20-2009, 01:57 PM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  4. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM

Tags for this Thread