I'm working on a ia64-machine using ICC 11.1. The following program compiles nicely:
#include <pthread.h>
#include <iostream>
using namespace std;
int main()
{
cout << PTHREAD_STACK_MIN << '\n';
return 0;
}
When I compile it with icc test.cpp -o test
BUT when I change the contents of the file to to:
#include <pthread.h>
#include <stdio.h>
int main()
{
printf("%d\n", PTHREAD_STACK_MIN);
return 0;
}
I suddenly get:
icc -c test.cpp -o test.o test.cpp(6):
error: identifier "PTHREAD_STACK_MIN"
is undefined
printf("%d\n", PTHREAD_STACK_MIN);
^
compilation aborted for test.cpp (code
2)
Can anyone explain to me why? Or more importantly: how I can work around this issue so that the second code example will also compile?
Well, that's easy: you forgot to include <limits.h> where the PTHREAD_STACK_MIN is supposed to be declared (as per POSIXv6/SUSv3).
And from the error one can conclude that <iostream> internally also includes the <limits.h> why in C++ mode the error doesn't happen.
Related
I'm currently trying to run this image segmentation program, but I'm having errors when I try to compile it. To compile, you just make, which runs the command
g++ -g -O3 -I. -o segment segment.cpp -lm
However, it runs into a compilation error -- its output is
In file included from /usr/include/wchar.h:887:0,
from /usr/include/c++/6/cwchar:44,
from /usr/include/c++/6/bits/postypes.h:40,
from /usr/include/c++/6/iosfwd:40,
from /usr/include/c++/6/ios:38,
from /usr/include/c++/6/istream:38,
from /usr/include/c++/6/fstream:38,
from ./pnmfile.h:27,
from segment.cpp:23:
/usr/include/x86_64-linux-gnu/bits/wchar2.h:448:3: error: #error "Assumed value of MB_LEN_MAX wrong"
# error "Assumed value of MB_LEN_MAX wrong"
^~~~~
Makefile:14: recipe for target 'segment' failed
make: *** [segment] Error 1
Here is the relevant area of /usr/include/x86_64-linux-gnu/bits/wchar2.h
/* We would have to include <limits.h> to get a definition of MB_LEN_MAX.
But this would only disturb the namespace. So we define our own
version here. */
#define __WCHAR_MB_LEN_MAX 16
#if defined MB_LEN_MAX && MB_LEN_MAX != __WCHAR_MB_LEN_MAX
# error "Assumed value of MB_LEN_MAX wrong"
#endif
if (__bos (__s) != (size_t) -1 && __WCHAR_MB_LEN_MAX > __bos (__s))
return __wcrtomb_chk (__s, __wchar, __ps, __bos (__s));
return __wcrtomb_alias (__s, __wchar, __ps);
}
And the relevant areas of segment.cpp:
#include <cstdio>
#include <cstdlib>
#include <image.h>
#include <misc.h>
#include <pnmfile.h> //<-- Line 23 of segment.cpp, where the error occurs
#include "segment-image.h"
and pnmfile.h:
#include <cstdlib>
#include <climits>
#include <cstring>
#include <fstream> //<-- Line 27 of pnmfile.h where the error occurs
#include "image.h"
#include "misc.h"
Does anyone know what's causing this error (and if so, how to fix it)? I'm running Ubuntu 16.04 with g++ 6.4.0. Thank you for any help you can give!
EDIT: I've confirmed that fstream does work. I compiled the following
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main() {
ifstream is("in.txt");
int a, b;
is >> a >> b;
cout << a << endl;
cout << b << endl;
return 0;
}
with the same command
g++ -g -O3 -I. -o temp.exe temp.cpp -lm
and it successfully printed out two integers read from a text file.
You need to include limits.h from the same libc from which your bits/wchar2.h is coming. If you check the comment in the latter file just above the #error which you are getting, you'll see why - it serves as a safeguard against mixing incompatible header files.
This kind of longtime issues is my motivation to build my own Linux distro.
Googling gets you nowhere. But ... building your own (cross) compiler
solves the problem. Compiler has it own definition in his relevant headers MB_LEN_MAX 1
MB_LEN_MAX 16 in glibc includes. No problem.
I have a C++ program which I am compiling on a x64 machine running Windows 10 with g++ x86_64-win32-seh-rev2 v7.1.0, using the command g++ -g main.cpp. When I run my program, I get the error 0xc000007b. This is my code
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
When I compile with this code
#include <stdio.h>
using namespace std;
int main() {
printf("Hello World");
return 0;
}
It works fine. When I run it in gdb, it runs fine.
I have seen other posts where there are dlls being used that do not support the architecture, but I don't think I am using any dlls in this application, unless they are being added by g++
I don't know the correct syntax for Dice.h and Dice.cpp to talk to my final game.cpp file.
I am getting a compile error that is returning a undefined reference to Dice::Dice()
Here are my 3 file headers
Game.cpp
#include "Dice.h"
#include <iostream>
using namespace std;
int main()
{ int sum;
Dice dice1;
Dice dice2;
dice1.roll();
dice2.roll();
sum = dice1.getFace() + dice2.getFace();
cout << sum;
return 0;
}
Dice.h
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <ctime>
// definition of class Dice
class Dice //... cont
Dice.cpp
#include "Dice.h"
using namespace std;
Dice::Dice() //... cont
I get the error when i compile by typing g++ -Wall -o game game.cpp
Is this the correct way to compile multiple files?
Just because you have the includes in the files doesn't mean they will be compiled together.
Try g++ -Wall -o game Dice.cpp Game.cpp
...I could be slightly wrong on the command but that should work
Side note you should really look into Makefiles. Makes everything so much easier.
The other answer is close, but slightly off. Enter
++ -Wall -o game game.cpp Dice.cpp
Make sure that game is lower case, and I think the file containing main() needs to go first.
This is a minimal program that I made to understand this problem better.
ADT.h
#ifndef ADT_H
#define ADT_H
class ADT {
public:
void print();
};
#endif
ADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
void ADT::print()
{
cout << "This program works." << endl;
}
testADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
int main(void)
{
ADT sa;
sa.print();
return 0;
}
I compiled it with the vim/minGW compiler my school provided me like so:
g++ testADT.cpp
Which produced the following error:
C:\Users\King\AppData\Local\Tempcc6eoWAP.o:testADT.cpp(.text+0x15 reference to 'ADT::print()'
collect2.exe error: ld returned 1 exit status
Can you explain this error message and indicate the error in my code?
You didn't post the error, but I see that you're missing the semicolon after void print()in the header.
EDIT: That's a linker error. Each source file should be compiled into an object file; then the object files linked:
g++ -c -oADT.o ADT.cpp
g++ -c -otestADT.o testADT.cpp
g++ -oADT ADT.o testADT.o
You can also do it in one line as in michaeltang's answer, but then you can't recompile the sources individually (the 2 step method scales better).
You should also compile ADT.cpp
g++ -o testadt testADT.cpp ADT.cpp
I am using Ubuntu 13.10. I am getting some errors for the following code.
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>
int main(int argc, char *argv[])
{
error.set_program_name(argv[0]);
if ( argc != 2 )
{
// printf(argv[0] + " usage: fifo_client [string] \n");
/// cout << argv[0] << " usage: fifo_client [string]" << endl;
exit(EXIT_FAILURE);
}
ofstream out(fifo_file);
if(out)
out << argv[1] << endl;
return(EXIT_SUCCESS);
}
If I run the above program a.c using command
gcc a.c -o a
a.c:1:20: fatal error: iostream: No such file or directory
#include <iostream>
^
compilation terminated.
I don't know whats the problem.
Use g++ instead of gcc. gcc could compile a c++ file if it had the right extension (.cpp for instance) or with the right arguments (-x c++) but adding the arguments needed to link with the C++ libraries is far too complex to avoid the simple solution.
The problem is that you're mixing C & C++ code and compiling it using GCC.
try
#include <fstream>
using namespace std;
instead of #include <fstream.h>
anyway your source code is not full to make correct suggestion.
I ran your code in my compiler and got following error :-
test2.c:3:21: fatal error: fstream.h: No such file or directory
#include <fstream.h>
^
compilation terminated.
so i think your question has typo.
It is because you are mixing c and c++ code, fstream is part of c++. try to run by g++.