Hello,
Thanks for providing community images of OpenVMS for x86! I'm trying to port a library which works on OpenVMS 8.4 (Alpha, IA64) to OpenVMS 9.2 (x86_64) using the latest community VMDK image. Right now I have a strange problem: when I compile a simple program with C compiler (basically, printing the size of long type), it shows that the size of long is 4 bytes, as on Alpha/IA64. However, the same program compiled with C++ compiler, outputs 8 bytes. How is that possible? Are there special compile options required to make C++ programs compatible with C?
Different size of long with C and C++ on x86
-
- Senior Member
- Posts: 531
- Joined: Fri Apr 17, 2020 7:31 pm
- Reputation: 0
- Location: Rhode Island, USA
- Status: Offline
- Contact:
Re: Different size of long with C and C++ on x86
Indeed. Very clever.
$ type cwhat.cpp
#include <stdio.h>
int main()
{
if(sizeof(' ') == sizeof(int))
{
puts("C compiler");
}
else if(sizeof(' ') == sizeof(char))
{
puts("C++ compiler");
}
else
{
puts("Houston we have a problem");
}
return 0;
}
$ clang cwhat.cpp
$ link cwhat
$ run cwhat
C++ compiler
$ copy cwhat.cpp *.c
$ clang cwhat.c
$ link cwhat
$ run cwhat
C compiler
Re: Different size of long with C and C++ on x86
sure, until you have to call a system/RTL routine that was compiled with our C compiler. Do a "scanf" into a C++ "long" and you'll only get 1/2 the value. And if you printf a "long" with "%ld", you'll only print 1/2 the value. The CRTL believes that "long" is 32-bits even when called from clang. We'll fix that.mberryman wrote: ↑Fri May 17, 2024 5:46 pmI have worked on a few packages that mix C and C++ code and which also share structs that include elements defined using long, size_t, etc. Compiling with the C and CXX compilers produced the obvious size mismatches. I solved this problem by simply compiling everything with clang. Since clang is both a C and C++ compiler, this also eliminates the need to make one's C code C++ clean.
Code: Select all
$ type scanf.cxx
#include <stdio.h>
#include <string>
int main () {
std::string inputstring = "1024 1024";
long l1;
long l2;
l1 = -1;
l2 = -1;
sscanf(inputstring.c_str(),"%ld %lld",&l1,&l2);
printf("l1 = %16llx, l2 = %16llx\n",l1,l2);
}
$ cxx scanf
DKA300:[jreagan]SCANF.CXX;7:10:43: warning: format specifies type 'long long *' but the argument has type 'long *' [-Wformat]
sscanf(inputstring.c_str(),"%ld %lld",&l1,&l2);
~~~~ ^~~
%ld
DKA300:[jreagan]SCANF.CXX;7:11:37: warning: format specifies type 'unsigned long long' but the argument has type 'long' [-Wformat]
printf("l1 = %16llx, l2 = %16llx\n",l1,l2);
~~~~~~ ^~
%16lx
DKA300:[jreagan]SCANF.CXX;7:11:40: warning: format specifies type 'unsigned long long' but the argument has type 'long' [-Wformat]
printf("l1 = %16llx, l2 = %16llx\n",l1,l2);
~~~~~~ ^~
%16lx
3 warnings generated.
$ link scanf
$ run scanf
l1 = ffffffff00000400, l2 = 400
-
- Senior Member
- Posts: 531
- Joined: Fri Apr 17, 2020 7:31 pm
- Reputation: 0
- Location: Rhode Island, USA
- Status: Offline
- Contact:
Re: Different size of long with C and C++ on x86
Little gotcha.
But that is also a C++ problem not just a "clang for C" problem.
I believe the C++ standard requires stdio.
Even though I believe the correct syntax nowadays are:
#include <cstdio>
instead of:
#include <stdio.h>
But that is also a C++ problem not just a "clang for C" problem.
I believe the C++ standard requires stdio.
Even though I believe the correct syntax nowadays are:
#include <cstdio>
instead of:
#include <stdio.h>