Thread: Compiling C code

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    Compiling C code

    long long is C++ data type however required to use in hello.c program. How one can compile C language program by u sing C++ compiler.........

    Code:
    #include <stdio.h>
    void fun(long long );
    
    int main()
    {
      long long j=20;
      fun(20);
    
    }
    
    void fun(long long k)
    {
      printf("%d",k);
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    "%lld" is the correct format specifier for long long ints, not "%d" (or if you're on Windows you should use "%I64d")

    Other than that though, what exactly is your problem??

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    Remove printf completely...Let the function remain empty..Program wont compile as long long is not part of C...its part of C++...Hence, the question is how to compile C program with C++ compiler

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    long long ints work using every C compiler I've ever tried.

    Once again, what exactly is your problem (as in, please post error messages etc...)?

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    It wont work in MS Visual Studio . You can try . You will get 3 compilation errors.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Really? It won't? 3 compilation errors exactly?

    I think you just blew my mind, because I swear I just compiled it fine. But that can't be possible. I'm staring at my Visual Studio window in shock. Don't worry though, I'm going to search harder. I'm sure I'll find those compilation errors eventually...

    Code:
    #include <stdio.h>
    void fun(long long );
    
    int main()
    {
      long long j=20;
      fun(20);
    }
    
    void fun(long long k)
    {
      printf("%d",k);
    }
    Code:
    1>------ Build started: Project: TestCPPConsole, Configuration: Release Win32 ------
    1>Compiling...
    1>main.cpp
    1>Linking...
    1>Generating code
    1>Finished generating code
    1>Embedding manifest...
    1>Build log was saved at "really/long/path"
    1>TestCPPConsole - 0 error(s), 0 warning(s)
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    arpsmack...

    main.cpp ( Visual Studio will compile it as C++ )
    main.c ( Visual Studio will compile it as C )

    also at the OP

    to use C code in a C++ enviroment
    Code:
    //main.cpp
    extern "C"
    {
    #include "main.c"
    }
    
    //bla bla
    Last edited by Raigne; 10-13-2008 at 01:13 AM.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    OHHH my mistake, so then I should have done this:

    Code:
    1>------ Build started: Project: TestCPPConsole, Configuration: Release Win32 ------
    1>Compiling...
    1>main.c
    1>Linking...
    1>Generating code
    1>Finished generating code
    1>Embedding manifest...
    1>Build log was saved at "really/long/path"
    1>TestCPPConsole - 0 error(s), 0 warning(s)
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    which also compiled with no errors. However, I guess I got confused by the language he was using, because I thought he was trying to compile it as C++ code.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    this is the most I get
    Code:
    1>------ Build started: Project: t, Configuration: Debug Win32 ------
    1>Compiling...
    1>t.c
    1>c:\documents and settings\cody doughty\my documents\visual studio 2008\projects\t\t\t.c(5) : warning C4007: 'main' : must be '__cdecl'
    1>c:\documents and settings\cody doughty\my documents\visual studio 2008\projects\t\t\t.c(6) : warning C4189: 'j' : local variable is initialized but not referenced
    1>Build log was saved at "file://c:\Documents and Settings\Cody Doughty\My Documents\Visual Studio 2008\Projects\t\t\Debug\BuildLog.htm"
    1>t - 1 error(s), 2 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    I can't repeat your results. I even went as far as to command-line compile manually passing the /TC flag to explicitly say I wanted every file compiled as C code.

    Code:
    D:\Documents\Visual Studio 2008\Projects\TestCPPConsole\TestCPPConsole>cl /TC main.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    main.c
    Microsoft (R) Incremental Linker Version 9.00.30729.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:main.exe
    main.obj
    [edit]
    Ok ok, I suppose I'm not being very constructive. Why don't you try using __int64 instead? Maybe that will work for you.
    [/edit]
    Last edited by arpsmack; 10-13-2008 at 01:34 AM.

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    From google'ing the error message you get (you should try it...), it looks like you need to add a calling convention specifier to your compile options. Not sure why it works for arpsmack, but not for you, but I guess that's dependent on the type of project you created (whether console application or other).

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    Thanks for your replies. Unfortunately, it's not working for me in Microsoft Visual Studio. I followed following steps

    File->New->TAB Files and added File Name: main.cpp ........................
    Code:
    //main.cpp
    
    
     extern "C"
    {
     #include "hello.c"
    
     }
    
    //one more file created as hello.c as below
    
    #include <stdio.h>
    void fun(long long a);
    
    int main()
    {
      long long j=20;
      fun(20);
    
       return 0;
    }
    
    void fun(long long k)
    {
    
    
    }
    Still, after pressing F5, I get 3 errors.

    ompiling...
    main.cpp
    C:\Documents and Settings\madhusudan_mothe\hello.c(2) : error C2632: 'long' followed by 'long' is illegal
    C:\Documents and Settings\madhusudan_mothe\hello.c(6) : error C2632: 'long' followed by 'long' is illegal
    C:\Documents and Settings\madhusudan_mothe\hello.c(12) : error C2632: 'long' followed by 'long' is illegal
    Error executing cl.exe.

    main.exe - 3 error(s), 0 warning(s)
    --------------------Configuration: main - Win32 Debug--------------------
    Compiling...
    main.cpp
    c:\\hello.c(2) : error C2632: 'long' followed by 'long' is illegal
    c:\\hello.c(6) : error C2632: 'long' followed by 'long' is illegal
    c:\\hello.c(12) : error C2632: 'long' followed by 'long' is illegal
    Error executing cl.exe.

    main.exe - 3 error(s), 0 warning(s)

    Plesae SUGGEST.

  13. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    What does hello.c contain?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    File contains are given in detail in my query. Please check the same.

  15. #15
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Well, firstly, you should never #include a .c file!

    Put all your code into one file for now, remove the extern "C" stuff and save the whole thing as a c File. Then compile that file and that file only.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop and compiling inside a code
    By MtJ in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 12:50 PM
  2. Replies: 1
    Last Post: 03-01-2006, 03:07 AM
  3. compiling c code
    By MadCow257 in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2006, 09:26 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM