Error "Error: stray character" when using C++11 - c++

I'm running into a strange issue when I try to compile the following simple C++11 code on my machine:
#include <boost/thread/thread.hpp>
It compiles fine with g++ foo.cpp -o foo but chokes on g++ -c -std=c++11 foo.cpp -o foo with the following error:
In file included from /usr/local/lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/functional:56:0,
from /usr/local/lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/memory:81,
from /usr/local/include/boost/config/no_tr1/memory.hpp:21,
from /usr/local/include/boost/smart_ptr/shared_ptr.hpp:27,
from /usr/local/include/boost/shared_ptr.hpp:17,
from /usr/local/include/boost/thread/pthread/thread_data.hpp:10,
from /usr/local/include/boost/thread/thread.hpp:17,
from foo.cpp:1:
./tuple:1:1: error: stray ‘\317’ in program
./tuple:1:1: error: stray ‘\372’ in program
./tuple:1:1: error: stray ‘\355’ in program
./tuple:1:1: error: stray ‘\376’ in program
... Additional lines omitted
I'm at a loss. Especially since I've been able to compile other programs against C++11 without any issue. Any ideas?
I'm running OS X v10.7.4 (Lion) and GCC 4.7.1.

That's because you have a file in your current directory named tuple that is included instead of the standard tuple header. Probably because of some -I. in the compilation line.
The strays characters in the error messages are simply the first bytes of the file not in the allowed character set, in octal.

Related

Defining strings in C++ causes a compiler error [duplicate]

It seems to me that gcc can deal with both c and c++ projects,so why is g++/gcc-c++ needed?
What's the difference between g++ and gcc-c++?
gcc will compile C source files as C and C++ source files as C++ if the file has an appropriate extension; however it will not link in the C++ library automatically.
g++ will automatically include the C++ library; by default it will also compile files with extensions that indicate they are C source as C++, instead of as C.
From http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b:
C++ source files conventionally use one of the suffixes .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx; C++ header files often use .hh, .hpp, .H, or (for shared template code) .tcc; and preprocessed C++ files use the suffix .ii. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).
However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and treats .c, .h and .i files as C++ source files instead of C source files unless -x is used, and automatically specifies linking against the C++ library. This program is also useful when precompiling a C header file with a .h extension for use in C++ compilations.
For example, to compile a simple C++ program that writes to the std::cout stream, I can use either (MinGW on Windows):
g++ -o test.exe test.cpp
gcc -o test.exe test.cpp -lstdc++
But if I try:
gcc -o test.exe test.cpp
I get undefined references at link time.
And for the other difference, the following C program:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int* new;
int* p = malloc(sizeof(int));
*p = 42;
new = p;
printf("The answer: %d\n", *new);
return 0;
}
compiles and runs fine using:
gcc -o test.exe test.c
But gives several errors when compiled using:
g++ -o test.exe test.c
Errors:
test.c: In function 'int main()':
test.c:6:10: error: expected unqualified-id before 'new'
test.c:6:10: error: expected initializer before 'new'
test.c:7:32: error: invalid conversion from 'void*' to 'int*'
test.c:10:9: error: expected type-specifier before '=' token
test.c:10:11: error: lvalue required as left operand of assignment
test.c:12:36: error: expected type-specifier before ')' token
As far as I know, g++ uses the correct C++ linker options whereas gcc uses the C linker options (so you may get undefined references, etc.).

qt #include <vector> error

I am trying to use std::vector in qt, but I seem to get some errors, When ever I include the vector header and compile the below code, I get a big list of errors but when I remove the vector header and compile the same code it works fine.
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
std::cout<<"Vector"<<std::endl;
return 0;
}
Since there were lot of errors I decided to put them into a file and provide the link below.
http://goo.gl/XOj0nV
The beginning and the end of the build log:
12:03:19: Running steps for project vector...
12:03:19: Configuration unchanged, skipping qmake step.
12:03:19: Starting: "/usr/bin/make"
g++ -c -pipe -g -Wall -W -fPIE -I../../Qt/5.3/gcc/mkspecs/linux-g++ -I../vector -I. -o main.o ../vector/
main.cpp
In file included from ../vector/main.cpp:2:0:
./vector:1:1: error: stray '\177' in program
./vector:1:1: error: stray '\1' in program
./vector:1:1: error: stray '\1' in program
./vector:1:1: error: stray '\1' in program
./vector:1:8: warning: null character(s) ignored [enabled by default]
./vector:1:1: error: stray '\2' in program
./vector:1:18: warning: null character(s) ignored [enabled by default]
./vector:1:1: error: stray '\3' in program
./vector:1:20: warning: null character(s) ignored [enabled by default]
./vector:1:1: error: stray '\1' in program
./vector:1:22: warning: null character(s) ignored [enabled by default]
./vector:1:1: error: stray '\205' in program
./vector:1:1: error: stray '\4' in program
./vector:1:1: error: stray '\10' in program
./vector:1:30: warning: null character(s) ignored [enabled by default]
[...]
./vector:115:880: warning: null character(s) ignored [enabled by default]
./vector:115:886: warning: null character(s) ignored [enabled by default]
In file included from ../vector/main.cpp:2:0:
File: /home/sanjayan/Documents/qt_vector_errors Page 76 of 76
./vector:1:2: error: 'ELF' does not name a type
In file included from ../vector/main.cpp:2:0:
./vector:28:655: error: 'j' does not name a type
In file included from ../vector/main.cpp:2:0:
./vector:61:28: error: expected declaration before '}' token
make: *** [main.o] Error 1
12:03:22: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project vector (kit: Desktop Qt 5.3 GCC 32bit)
When executing step 'Make'
12:03:22: Elapsed time: 00:03.
I hope the provided information's is sufficient for my query.
I had the same problem like you just now.
I write a C++ program named vector.cc
#include <iostream>
#include <vector>
using namespace std;
int main()
{
cout << "hello" << endl;
return 0;
}
Compilation can pass for the first time (use g++ vector.cc -o vector, creates an executable file named vector), and then any program include the sentence #include vector will face the problems as you said.
The basic reason is the vector file, if the executable file (named "vector") is deleted, everything will be OK.
I hope my answer can help you.
If that problem arises not on every host then probably difference in the compiler. Difference is likely in the header files directory scan order. Such error should not arise if standard headers directory scanned before others.
As a workaround its enough to remove project target file manually befor building target. But this is not a programmers way (which are very lazy creatures).
Programmers way - is to learn QMake to delete target file just before project sources compilations (but under this way link phase will be done permanently). For me (QMake 2.01a under Linux) this is achived by addition of following lines into project file:
deltarget.commands = $$QMAKE_DEL_FILE $$TARGET
QMAKE_EXTRA_TARGETS += deltarget
PRE_TARGETDEPS += deltarget
This declares target deltarget which becomes built before current project. This sample uses undeclared in QMake's manual variable QMAKE_DEL_FILE which used to remove files (its content of course platfrom-dependent).

Git breaks my program?

I have several programs that I have been writing under a master branch, which I compile using GNUmakefile. Today, right after I created a new branch, v4.3, and switched onto the new branch, and then compile, I get the following error message:
lucerne:lucerne$ make primes
g++-mp-4.8 -std=gnu++0x -g -O2 -W -Wall -Wextra -MD -MF .deps/primes.d -MP -I. -c -o primes.o primes.cpp
In file included from primes.cpp:2:0:
./vector:1:1: error: stray '\317' in program
????_TEXT
^
./vector:1:1: error: stray '\372' in program
./vector:1:1: error: stray '\355' in program
./vector:1:1: error: stray '\376' in program
./vector:1:1: error: stray '\7' in program
./vector:1:6: warning: null character(s) ignored [enabled by default]
./vector:1:1: error: stray '\1' in program
./vector:1:1: error: stray '\3' in program
./vector:1:10: warning: null character(s) ignored [enabled by default]
./vector:1:1: error: stray '\200' in program
I get the same error message when I switch to master branch. However, if I rename the file to something else, like "new_primes.cpp", then make file does not cause any problem. Neither GNUmakefile or prime.cpp has been modified. I also have not installed any additional packages. It is also not the editor - I am using emacs and TextWrangler. What is causing this problem?
The error generated by GCC occurs when the source file, or in this case, included file is a binary file. This is easily reproduced by creating a source file with a single include to a binary file, /bin/ls for example. Create stray.c with:
#include "/bin/ls"
A quick complie:
gcc -c stray.c
Yields the following output:
In file included from stray.c:1:0:
/bin/ls:1:1: error: stray '\177' in program
/bin/ls:1:1: error: stray '\2' in program
/bin/ls:1:1: error: stray '\1' in program
/bin/ls:1:1: error: stray '\1' in program
/bin/ls:1:8: warning: null character(s) ignored [enabled by default]
/bin/ls:1:1: error: stray '\2' in program
/bin/ls:1:18: warning: null character(s) ignored [enabled by default]
It appears that 'vector' is a binary file.
There are two possible causes that come to mind:
The C++ 'vector' header file is corrupt.
There is another 'vector' in your search path.
I'm guessing its the second. Check and make sure you don't have a binary called vector within the compiler include path.

Compiler error in socket.h on Amazon EC2

I'm trying to compile bitcoin on my EC2 instance, and I've run into a problem I can't figure out. The build script stops on the following command
g++ -c -Wall -Wextra -Wformat -Wformat-security -Wno-unused-parameter -g -DMSG_NOSIGNAL=0 -DBOOST_SPIRIT_THREADSAFE -DUSE_UPNP=1 -DUSE_IPV6=1 -I/home/ec2-user/bitcoin/src/leveldb/include -I/home/ec2-user/bitcoin/src/leveldb/helpers -DHAVE_BUILD_INFO -I"/home/ec2-user/bitcoin/src" -I"/home/ec2-user/bitcoin/src/obj" -I"/usr/local/include" -I"/usr/include/openssl" -MMD -MF obj/alert.d -o obj/alert.o alert.cpp
by returning the following error(s)
In file included from /usr/include/sys/socket.h:40:0,
from compat.h:19,
from netbase.h:11,
from util.h:27,
from alert.h:13,
from alert.cpp:11:
/usr/include/bits/socket.h:231:5: error: expected identifier before numeric constant
/usr/include/bits/socket.h:231:5: error: expected ‘}’ before numeric constant
/usr/include/bits/socket.h:231:5: error: expected unqualified-id before numeric constant
In file included from compat.h:19:0,
from netbase.h:11,
from util.h:27,
from alert.h:13,
from alert.cpp:11:
/usr/include/sys/socket.h:254:1: error: expected declaration before ‘}’ token
I've tried compiling with the -std=c++0x option set, but it made no difference. That was the only thing I've been able to come up with.
I'd wager that some header file you have is #defineing a macro that interferes with socket.h. Are you able to compile a program that only includes <sys/socket.h>, with no other inclusions?
The next thing to check is to look at /usr/include/bits/socket.h and see what's on line 231 (where the first error occurs). If the code looks ok, then the next step is to see what the preprocessed source looks like. To get the preprocessed output, replace the -c option with -E on the command line, and change the -o obj/alert.o option to -o alert.ii to put the preprocessor output into the file alert.ii.
If you compare the content of alert.ii with /usr/include/bits/socket.h, you can see if it's getting compiled as expected or not. In particular, if there's a macro which defines something into something unexpected, you'll see code which is clearly wrong at the location the compiler is pointing out.

How to add speech.h to g++ in Cygwin?

I'm just trying to compile a c++ program using the emulated g++ compiler in Cygwin on a Windows 7 machine that has the following includes:
iostream
string
windows.h
stdio.h
mmsystem.h
initguid.h
objbase.h
objerror.h
ole2ver.h
speech.h
It compiles them all fine with the exception of speech.h, which is sort of the bread and butter of what I'm working on, soo.. yeah.
Returns the following:
test.cpp:11:20: fatal error: speech.h: No such file or directory compilation terminated.
Any and all help will be much appreciated.
The files that do compile are on your system path, so the compiler can find them. speech.h isn't, so you have to tell it where to look:
g++ -c test.cpp -I<Path_to_speech.h>/speech.h ...
I.e. if it's in C:\Users\Kirk\test\include, then
g++ -c test.cpp -IC:/Users/Kirk/test/include/speech.h ...