-
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);
}
-
"%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??
-
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
-
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...)?
-
It wont work in MS Visual Studio . You can try . You will get 3 compilation errors.
-
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 ==========
-
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
-
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.
-
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 ==========
-
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]
-
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
-
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.
-
What does hello.c contain?
QuantumPete
-
File contains are given in detail in my query. Please check the same.
-
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