Results 1 to 10 of 21
Hi all,
I'm a beginner in linux, and I face some problems in compiling a c++ code of mine under linux.
Actually my code works perfectly under windows (under visual ...
- 05-05-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 14
Compiling a c++ code
Hi all,
I'm a beginner in linux, and I face some problems in compiling a c++ code of mine under linux.
Actually my code works perfectly under windows (under visual studio 200
.
My code uses some library, that has some header files, and some .cpp files.
My code (a file called program.cpp) and the .cpp and .h files of the library I'm using are all included in the attached .zip file.
Under linux, I put all those files in one directory in linux, and I used gcc program.cpp, but I got a lot of errors.
I'd appreciate it a lot, if someone could help me, and tell me exactly what to do/modify in order to have my code work perfectly under linux.
Thanks a lot.
Aly
- 05-05-2009 #2
Make sure to use
g++ program.cpp -o program
for C++ programs...G4143Make mine Arch Linux
- 05-05-2009 #3Just Joined!
- Join Date
- May 2009
- Posts
- 14
Thanks, but still have problems
Hi Gerard,
Thanks a lot for your reply. I tried what you suggested, and I got the following:
Code:In file included from /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward/iostream.h:31, from include.h:138, from program.cpp:12: /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated. program.cpp:260:2: warning: no newline at end of file /tmp/ccNrW27b.o: In function `main': program.cpp:(.text+0xc6d): undefined reference to `DiscreteGen::DiscreteGen(int, double*, double*)' program.cpp:(.text+0xca7): undefined reference to `Random::Set(double)' program.cpp:(.text+0xcf2): undefined reference to `DiscreteGen::Next()' program.cpp:(.text+0xd6f): undefined reference to `DiscreteGen::~DiscreteGen()' program.cpp:(.text+0xd88): undefined reference to `DiscreteGen::~DiscreteGen()' collect2: ld returned 1 exit status
All those "undefined reference to" are functions from the files of the library.
What should I do next?
Thanks a lot.
Aly
- 05-05-2009 #4Make mine Arch Linux
- 05-05-2009 #5Just Joined!
- Join Date
- May 2009
- Posts
- 14
Those are the headers I'm using:
Wherer include.h, newran.h, tryrand.h are three header files from that library that I included its .h and .cpp files in the same folder as my code, which is in program.cpp.Code:#include <fstream> #include <iostream> #include <stdio.h> #include <string> #include <cstdlib> using namespace std; #define WANT_STREAM #define WANT_MATH #define WANT_TIME #include "include.h" #include "newran.h" #include "tryrand.h" #ifdef use_namespace using namespace NEWRAN; #endif
Also, note that I included iostream, not iostream.h!!!!
So??!!!!!
Aly
- 05-05-2009 #6
I'm no C++ expert but should you be mixing stdio.h and iostream in the same program? stdio.h is C input/output header file and iostream is C++ input/output header file....Gerard4143
Make mine Arch Linux
- 05-05-2009 #7
- 05-05-2009 #8Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
There are a couple of problems here. One is that GCC is quite strict about properly terminating lines, even the last one in a source file. Hence the warning:
program.cpp:260:2: warning: no newline at end of file
Just edit the file program.cpp, go to the end of the last line, and enter a new-line, save, and that problem is fixed. This is usually a remnant of transferring source files from Windoze to Linux/Unix. It (the missing new-line) is, in fact, a violation of the ANSI C standard.
The warning about a deprecated header:
This is because the header file "include.h" is including iostream.h, yet you are using the new-style #include <iostream> in your source file. Hence the conflict. You need to fix the local include files "include.h", "newran.h", and "tryrand.h" as necessary.Code:In file included from /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward/iostream.h:31, from include.h:138, from program.cpp:12: /backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++
The major problem is the undefined references error which means that the object files or libraries that contain the implementation of these symbols are not linked to the program, or you have specified the caller after the libraries were searched. This is not an uncommon problem, and usually requires understanding what order to place the libraries in on the link line.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-05-2009 #9Just Joined!
- Join Date
- May 2009
- Posts
- 14
Hi Rubberman,
Thanks a lot for your help and support.
I actually solved the first problem, but couldn't solve the second and third ones.
As for the second one, what should I exactly do in these 3 local include files?
And for the third one, I don't know what should be placed in which order (thaey actually work in that order under windows), so, should I just try different orders of the include lines in my program.cpp file? or what should I do?
Thank you so much for your support!
Aly
- 05-05-2009 #10Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
1. Edit the include files mentioned and change things like;
#include <iostream.h>
to
#include <iostream>
2. This is not an include issue (using the -I <directory>) but a linkage issue using the -L <dirname> -l <libname> directives. How are you building this program - just by compiling with g++ on the command line? Or in a Makefile?Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
