GCC + STL Compile Problem
Hi all,
I can't get the following code to compile under SUSE Linux 9.1, using GCC 3.3.3 or GCC 3.3.1, what am I missing here? This is my example CPP: #include <iostream> #include <string> int main(int argc,char **argv) { std::string testString= "hello world"; std::cout << testString<< std::endl; return 0; } and the Makefile is: TestSTL: TestSTL.o ld -L"/usr/lib" -L"/usr/local/lib"\ TestSTL.o\ -o TestSTL TestSTL.o: TestSTL.cpp gcc -g -O0 -I"." -I"/usr/include/g++" -I"/usr/include/g++/bits"\ -I"/usr/include/g++/ext"\ -c TestSTL.cpp -o TestSTL.o clean: rm -f *.o rm TestSTL If anyone would be so nice as to correct the code or makefile, I would be grateful! It looks so simple, and it works under Windows using VC 2003 and VS 2005 Beta. However GCC 3.3.1 Cygwin on Windows fails too. Greetings. |
There's nothing wrong with the code itself. The problem is that you're using gcc to compile a c++ program. Use g++ instead, that's the c++ compiler in gcc (gnu compiler collection). GCC contains a number of compilers and on some systems, the compilers for each language must be installed separately (I know this is the case in Cygwin). And btw, that's a very complicated Makefile for that program and you shouldn't have to set paths like that if your system is setup correctly.
if your program source file name test.cpp, you could simply do: g++ -o test test.cpp (run with $ ./test) or just make test (no Makefile required) But I like to compile mine with -W and -Wall to get more warnings about potential problems. A Makefile could look like (for a c++ program): CXX = g++ LD = g++ CXXFLAGS = -Wall -W -ansi -pedantic -g -ggdb -c -o LDFLAGS = -o $(EXEC) EXEC = test OBJECTS = test.o all: $(OBJECTS) <tab>$(LD) $(OBJECTS) $(LDFLAGS) %.o: %.cpp <tab>$(CXX) $(CXXFLAGS) $@ $< clean: <tab>rm -f $(OBJECTS) $(EXEC) This is the makefile template I use for most of my c++ programs (when making debug versions). It's not necessary for a program with only one source file. If you need to link with any libraries not in your path, add those to LDFLAGS. |
Not quite there yet...
Hi again,
thanks for the advice regarding the compiler and the makefile. However, after replacing the gcc call with g++ I get the following output from the compile, which is the same as before: > make TestSTL.o g++ -g -O0 -I"." -I"/usr/include/g++" -I"/usr/include/g++/bits"\ -I"/usr/include/g++/ext"\ -c TestSTL.cpp -o TestSTL.o In file included from /usr/include/g++/ext/memory:67, from /usr/include/g++/string:48, from /usr/include/g++/bits/locale_classes.h:47, from /usr/include/g++/bits/ios_base.h:47, from /usr/include/g++/ios:49, from /usr/include/g++/ostream:45, from /usr/include/g++/iostream:45, from TestSTL.cpp:1: /usr/include/g++/bits/stl_tempbuf.h:78: error: type specifier omitted for parameter `_ForwardIteratorConcept' /usr/include/g++/bits/stl_tempbuf.h:80: error: parse error in method specification before `_M_original_len' /usr/include/g++/bits/stl_tempbuf.h:144: error: syntax error before `}' token In file included from /usr/include/g++/string:48, from /usr/include/g++/bits/locale_classes.h:47, from /usr/include/g++/bits/ios_base.h:47, from /usr/include/g++/ios:49, from /usr/include/g++/ostream:45, from /usr/include/g++/iostream:45, from TestSTL.cpp:1: /usr/include/g++/ext/memory:69: error: semicolon missing after declaration of ` std::_Temporary_buffer<_ForwardIterator, _Tp>' /usr/include/g++/bits/stl_tempbuf.h:80: error: parse error at end of saved function text /usr/include/g++/ext/memory:70: error: ISO C++ forbids defining types within return type /usr/include/g++/ext/memory:70: error: syntax error before `{' token /usr/include/g++/ext/memory:70: confused by earlier errors, bailing out make: *** [TestSTL.o] Fehler 1 I got this example to compile, but not link, using the new Intel C++ Compiler v8.1 (icgc). Did anyone manage to compile the code given above successfully? I would really need to get the changed CPP or Makefile, so that I can try it out on my box here. Thanks in advance. |
Can you compile your program with just
g++ -o foo <nameofyoursourcefileincludingextension> ? |
That works fine!
Hello,
using g++ -o foo TestSTL.cpp works fine for me. It compiles, links & runs without problems. Needless to say, i'm a bit gobsmacked at this point! Do you know why this works and the other method using the makefile doesn't? Yours questioningly. |
Solved!
As far as I can tell, the problems were:
1) Using gcc instead of g++ 2) Including the following into the Makefile: -I"/usr/include/g++" -I"/usr/include/g++/bits"\ -I"/usr/include/g++/ext"\ 3) Using ld instead of calling g++ again with just the *.o files as such: # makefile for TestSTL TestSTL: TestSTL.o g++ -L"/usr/lib" -L"/usr/local/lib" TestSTL.o -o TestSTL TestSTL.o: TestSTL.cpp g++ -g -O0 -I"." -c TestSTL.cpp -o TestSTL.o clean: rm -f *.o rm TestSTL Although I don't understand the cause & effect here, I can continue development on my main issue, so thankyou very much for your help! Regards. |
Yeah, that's another problem when one tries to use ld directly to perform the link, never do that. If you're writing C code, always set gcc as LD in your makefile. If you're writing c++ code, always set g++ as LD in your makefile (they will invoke ld itself when performing the actual link and they now how to invoke it properly). And don't try to include paths to libraries or headers and whatnot that are visible by default to your compiler.
Glad things are working for you now. |
All times are GMT -5. The time now is 03:17 AM. |