Incuding C++ header files in C, VS 2010

This is a discussion on Incuding C++ header files in C, VS 2010 within the C Programming forums, part of the General Programming Boards category; I want to write a C code, say "test.c" call some C++ functions from it. I have a header file ...

  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
    22,311
    Read this FAQ: How to mix C and C++
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,311
    Well, can you just compile your project as C++?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,311
    Do you need C++ classes to be defined in the header file in the first place?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,311
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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!
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 02: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, 03:50 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21