Like Tree2Likes

include c++ in c

This is a discussion on include c++ in c within the C++ Programming forums, part of the General Programming Boards category; The program I'm writing is in C++ except from the main function which is written in C.So it contains multiple ...

  1. #1
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116

    include c++ in c

    The program I'm writing is in C++ except from the main function which is written in C.So it contains multiple .cpp , one .c , and multiple .h
    The compiler complains with fatal errors that it cannot open included .h in .c
    How can I solve this?Something like extern is needed I guess
    but I have absolutely no idea how to use it!
    thanks in advance

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Which compiler? And what are these errors or warnings it's giving you? Can you give us more details on your project's structure and layout?

    We're NOT mind-readers.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    yes you're right I'm sorry,I just thought it was a general procedure to mix c with c++.
    Anyway I'm using Visual Studio 2008 and the error I'm getting is :

    fatal error C1083: Cannot open include file: 'A.h': No such file or directory

    I have a .c file where is the main function and includes :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "A.h"         
    #include "B.h"         
    #include "C.h"    
    #include "D.h"
    the A.h has functions declarations and doesn't include anything.

    the B.h and the C.h have also functions declarations and include the following:

    Code:
    #include "A.h"
    #include "D.h"
    #include "E.h"
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    
    using namespace std;
    the D.h has only a struct definition and includes

    Code:
    #include <stdio.h>
    #include <iostream>
    the E.h has a class definition and includes

    Code:
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include "A.h"
    #include "D.h"
    #include "F.h"
    
    using namespace std;
    the F.h has a class definition as well and includes:

    Code:
    #include <stdio.h>
    #include <string>
    #include <iostream>
    #include <stdlib.h>
    #include "A.h"
    #include "D.h"
    #include "E.h"
    
    using namespace std;

    and then are the .cpp files which have either functions or class functions depending on their header file.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Inability to #include "A.h" has nothing to do with mixing C and C++. It means that the compiler cannot find A.h. If a C compiler cannot find a file, a similarly configured C++ compiler (eg from the same vendor) will not find that file either.

    Given that all of your headers (A.h, B.h, etc) are #include'ing C++ headers, and your main.c #includes those headers, you cannot expect your main function to compile as C. There is no way to get a C compiler to accept C++ code/syntax unless it is actually a C++ compiler. And C++ standard headers almost invariably contain C++ code.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by grumpy View Post
    Inability to #include "A.h" has nothing to do with mixing C and C++. It means that the compiler cannot find A.h. If a C compiler cannot find a file, a similarly configured C++ compiler (eg from the same vendor) will not find that file either.

    Given that all of your headers (A.h, B.h, etc) are #include'ing C++ headers, and your main.c #includes those headers, you cannot expect your main function to compile as C. There is no way to get a C compiler to accept C++ code/syntax unless it is actually a C++ compiler. And C++ standard headers almost invariably contain C++ code.
    so what do you suggest?

  6. #6
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Asides from that cute circular dependency you have going on between "E.h" and "F.h"...

    You can compile most C code with C++ compiler. The reverse is not true.

    First, have you configured additional include directories for your project? Not having this would account for compiler's inability to find them, if they don't live in the same directory as the source file that includes them. Second, have you configured this project to compile as C or C++? It needs to be set to compile as C++ for this to have any chance of working and you need to use the C++ versions of standard C headers. Their names are the same as C's, but with `c` prefixed and `.h` extension dropped. E.g. `stdlib.h` becomes `cstdlib`, and `stdio.h` becomes `cstdio`. Note that mixing functions from `cstdio` and `iostream` will eventually lead to breakage in ways most weird. Thirdly, are you using `#pragma once` in your header files? It's a reasonably good idea to do that.

    With that said, what you're doing is painful to look at. :S
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Quote Originally Posted by quo View Post
    so what do you suggest?
    Compile all of your code as C++. The bulk of C code is valid C++ code. The reverse is not true. And most of the code in your project appears to be C++ anyway.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by msh View Post
    Asides from that cute circular dependency you have going on between "E.h" and "F.h"...

    You can compile most C code with C++ compiler. The reverse is not true.

    First, have you configured additional include directories for your project? Not having this would account for compiler's inability to find them, if they don't live in the same directory as the source file that includes them. Second, have you configured this project to compile as C or C++? It needs to be set to compile as C++ for this to have any chance of working and you need to use the C++ versions of standard C headers. Their names are the same as C's, but with `c` prefixed and `.h` extension dropped. E.g. `stdlib.h` becomes `cstdlib`, and `stdio.h` becomes `cstdio`. Note that mixing functions from `cstdio` and `iostream` will eventually lead to breakage in ways most weird. Thirdly, are you using `#pragma once` in your header files? It's a reasonably good idea to do that.

    With that said, what you're doing is painful to look at. :S
    yes I use #pragma once and I have them all in the same folder.I'll do all the rest you told me.Thanks for the suggestions!

  9. #9
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by grumpy View Post
    Compile all of your code as C++. The bulk of C code is valid C++ code. The reverse is not true. And most of the code in your project appears to be C++ anyway.
    that's what I'll do.thanks a lot.
    Does the same problem occur in a Linux environment?

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by quo View Post
    Does the same problem occur in a Linux environment?
    What problem ?
    Nowhere would a C compiler compile C++ code.
    Manasij Mukherjee | gcc-4.8.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 !



  11. #11
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by manasij7479 View Post
    What problem ?
    Nowhere would a C compiler compile C++ code.
    Ok so back in Visual Studio..
    I have a c++ project and .cpp .c and .h files
    the c++ compiler compiles the .c
    I use #pragma once for the includes
    It gives an error though in .c for library <cstdio> even when I change it to stdio.h
    It keeps returning syntax errors in the library


    cstdio(42) : error C2143: syntax error : missing '{' before ':'
    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'
    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2143: syntax error : missing '{' before ':'
    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'
    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'
    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2059: syntax error : ':'
    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'

  12. #12
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You have a syntax error in one of your includes and it's spilling over into code for next include. Probably a missing closing brace or semicolon. Guessing from how your includes are arranged, I'd take a look at "E.h".
    Last edited by msh; 05-10-2012 at 05:53 AM.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > The program I'm writing is in C++ except from the main function which is written in C
    Then you have a complete C++ program.
    [32] How to mix C and C++ ..Updated!.., C++ FAQ

    > cstdio(42) : error C2143: syntax error : missing '{' before ':'
    > 1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'

    If you've got this
    #include "A.h"
    #include "D.h"
    #include "E.h"
    #include <stdio.h>
    then you need to look at the end of E.h to see what kind of mess it is in.

    Like most everyone else who has commented, you really need to sort out all these cyclic dependencies.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    quo
    quo is offline
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by msh View Post
    You have a syntax error in one of your includes and it's spilling over into code for next include. Probably a missing closing brace or semicolon. Guessing from how your includes are arranged, I'd take a look at "E.h".

    Actually I renamed my .c to .cpp and it doesn't return the errors now.
    But although I included <stdlib.h>
    when I call drand48() in the main function(now it's main.cpp but used to be main.c)it doesn't see it and returns:

    error C3861: 'drand48': identifier not found

    is this because I renamed it?
    Last edited by quo; 05-10-2012 at 07:36 AM.

  15. #15
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by quo View Post
    is this because I renamed it?
    Yes. The c compiler propably never even got that far to see that call.

    Now your problem seems to be that MS standard library doesn't have any drand48().
    BTW if you want to compile as C++ then you should use
    Code:
    #include <cstdlib>
    instead of stdlib.h
    Kurt

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does #include indirectly include the source file too?
    By Lord Asriel in forum C Programming
    Replies: 10
    Last Post: 11-30-2011, 07:20 AM
  2. Replies: 1
    Last Post: 11-06-2011, 05:20 PM
  3. basic difference between #include<> and #include""
    By gunjansethi in forum C Programming
    Replies: 1
    Last Post: 03-26-2010, 12:53 AM
  4. #include <windows.h> and #include <wininet.h>
    By steve1_rm in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2009, 11:14 AM
  5. Which came first? #include <stdio.h> or #include <stdlib.h> ?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-14-2002, 09:58 PM

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