Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
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 ...
  1. #1
    Just 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
    Attached Files Attached Files

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Make sure to use

    g++ program.cpp -o program

    for C++ programs...G4143
    Make mine Arch Linux

  3. #3
    Just 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

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by amegahed3 View Post
    Hi all,

    Actually my code works perfectly under windows (under visual studio 200.

    Aly
    Do you have Windows specific header files included in your project? If you do they will not link in Linux...Only the standard libraries are considered portable...G4143

    Plus your warning messages states that your using iostream.h while it should be iostream
    Make mine Arch Linux

  5. #5
    Just Joined!
    Join Date
    May 2009
    Posts
    14
    Those are the headers I'm using:


    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
    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.

    Also, note that I included iostream, not iostream.h!!!!

    So??!!!!!

    Aly

  6. #6
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    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

  7. #7
    Linux Guru Rubberman's Avatar
    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
    Quote Originally Posted by gerard4143 View Post
    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
    Nothing wrong with that, though not really necessary. However, if you want to use functions like fprintf() and sprintf() you still need stdio.h.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Linux Guru Rubberman's Avatar
    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
    Quote Originally Posted by amegahed3 View Post
    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
    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:
    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++
    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.

    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!

  9. #9
    Just 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

  10. #10
    Linux Guru Rubberman's Avatar
    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
    Quote Originally Posted by amegahed3 View Post
    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
    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!

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...