compiling on linux -- gives error

Status
Not open for further replies.

russ-iha

Ars Praefectus
4,407
Subscriptor
OK this same code compiles and runs just fine on VC++ 6.0, but chokes on Linux using GCC. To my knowledge, it is not using any Windows-specific libraries or functions.

Here is the output the compiler generates if i do a plain "gcc *.cpp":

/tmp/ccS4QB6g.o: In function `main':
/tmp/ccS4QB6g.o(.text+0x179): undefined reference to `cout'
/tmp/ccS4QB6g.o(.text+0x17e): undefined reference to `ostream::eek:perator<<[char const *)'
/tmp/ccS4QB6g.o: In function `Log(basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >)':
/tmp/ccS4QB6g.o(.text+0x4b3): undefined reference to `cout'
/tmp/ccS4QB6g.o: In function `PrintArray(int *, int)':
/tmp/ccS4QB6g.o: In function `basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::Rep::copy(unsigned int, char const *, unsigned int)':
/tmp/ccS4QB6g.o(.basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::Rep::gnu.linkonce.t.copy(unsigned int, char const *, unsigned int)+0x33): undefined reference to `string_char_traits<char>::copy(char *, char const *, unsigned int)'
/tmp/ccS4QB6g.o: In function `basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::Rep::move(unsigned int, char const *, unsigned int)':
/tmp/ccS4QB6g.o(.basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::Rep::gnu.linkonce.t.move(unsigned int, char const *, unsigned int)+0x33): undefined reference to `string_char_traits<char>::move(char *, char const *, unsigned int)'
/tmp/ccS4QB6g.o: In function `basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::replace(unsigned int, unsigned int, char const *, unsigned int)':
/tmp/ccS4QB6g.o(.basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::gnu.linkonce.t.replace(unsigned int, unsigned int, char const *, unsigned int)+0x31): undefined reference to `__out_of_range(char const *)'
/tmp/ccS4QB6g.o(.basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::gnu.linkonce.t.replace(unsigned int, unsigned int, char const *, unsigned int)+0x8e): undefined reference to `__length_error(char const *)'
/tmp/ccS4QB6g.o: In function `__ls__H3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b1i0_R7ostreamRCt12basic_string3ZX01ZX11ZX21_R7ostream':
/tmp/ccS4QB6g.o(.gnu.linkonce.t.__ls__H3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b1i0_R7ostreamRCt12basic_string3ZX01ZX11ZX21_R7ostream+0x32): undefined reference to `ostream::write(char const *, int)'
/tmp/ccS4QB6g.o: In function `basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::assign(char const *)':
/tmp/ccS4QB6g.o(.basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::gnu.linkonce.t.assign(char const *)+0x16): undefined reference to `string_char_traits<char>::length(char const *)'
collect2: ld returned 1 exit status



Here is the main program... it is very simple as you can see.

code:<HR>
Code:
//main program file


// includes
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "sortlib.h"


using namespace std;


// function prototypes
void Log(string);
void PrintArray(int[], int);


// constants
const int SORTSIZE = 100;



int main() {

	
	// first create arrays and variables
	int tArray1[SORTSIZE];
	int tArray2[SORTSIZE];
	int tArray3[SORTSIZE];
	int tArray4[SORTSIZE];
	int tInt = 0;
	int t = 0	;
	int mnuInt = 1;
	string tString;

	
	// init the random number generator
	srand( (unsigned) time(NULL) );


	// fill arrays with test data
	Log("\n\nRandom Number Generator Seeded; filling array with elements\n\n");
	for(t; t < SORTSIZE; t++) {
		tInt = rand();
		tArray1[t] = tInt;
		tArray2[t] = tInt;
		tArray3[t] = tInt;
		tArray4[t] = tInt;
		Log(tString);
	}

	// call sorting methods
	//		BubbleSort(int[])		sort1.cpp
	//		QuickSort(int[])		sort2.cpp
	//		RadixSort(int[])		sort3.cpp
	//		MergeSort(int[])		sort4.cpp

	while(mnuInt > 0 && mnuInt < 5) {
		cout << "\n\n=========================";
		cout << "\nSort Library Test Menu\n";
		cout << "=========================\n";
		cout << "\t1. Bubble Sort\n";
		cout << "\t2. Quick Sort\n";
		cout << "\t3. Heap Sort\n";
		cout << "\t4. Merge Sort\n";
		cout << "\n\n\nPlease enter 1-4 (or any other number to exit): ";
		cin >> mnuInt;
		cout << "\n\n===========================================================================";

		switch(mnuInt) {
			case 1:
				Log("Running Bubble Sort...\n");
				BubbleSort(tArray1, SORTSIZE - 1);
				PrintArray(tArray1, SORTSIZE);
				break;
			case 2:
				Log("\nRunning Quick Sort...\n");
				QuickSort(tArray2, 0, SORTSIZE - 1);
				PrintArray(tArray2, SORTSIZE);
				break;
			case 3:
				Log("\nRunning Heap Sort...\n");
				HeapSort(tArray3, 0, SORTSIZE - 1);
				PrintArray(tArray3, SORTSIZE);
				break;
			case 4:
				Log("\nRunning Merge Sort...\n");
				MergeSort(tArray4, 0, SORTSIZE - 1);
				PrintArray(tArray4, SORTSIZE);
				break;
		}
		// we are done
		Log("\n\n\nDone Sorting!\n");
		Log("===========================================================================\n");
	} //end of our while loop


	return 0;
}


void Log(string LogText) {
	cout << LogText;
}


void PrintArray(int x[], int n) {
	int t = 0;
	int cols = 0;

	for(t; t < n; t++) {
		cout << "\t" << x[t];
		cols++;
		if(cols > 7) {
			cols = 0;
			cout << "\n";
		};
	};
}

Here's one of the associated files:

code:<HR>
Code:
//bubble sort


void BubbleSort(int x[], int n) {
// args: array, num in array
	int newLowest  = 0;    // index of first comparison
    int newHighest = n-1;  // index of last comparison
    
    while (newLowest < newHighest) {
       int highest = newHighest;
       int lowest  = newLowest;
       newLowest = n; 
       for (int i=lowest; i<highest; i++) {
          if (x[i] > x[i+1]) {
             // exchange elements
             int temp = x[i];  x[i] = x[i+1];  x[i+1] = temp;
             if (i<newLowest) {
                newLowest = i-1;
                if (newLowest < 0) {
                   newLowest = 0;
                }
             }else if (i>newHighest) {
                newHighest = i+1;
             }
          }
       }
    }
}



I can't see any problems here, but I am not that experienced with Unix/Linux systems and their programming.


I have tried compiling each one separately with -c (ex: "gcc -c tsort.cpp"); it is able to compile and assemble the files into .O files just fine.

But when i try to link the .o files together, I get similar errors.


Any help would be greatly appreciated.
 
Status
Not open for further replies.