Thread: Linker Errors undefined reference to 'rand'

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Linker Errors undefined reference to 'rand'

    Hi guys

    Can someone please help me out with this. when I try to compile the program below I get the following errors and I have no idea why.

    LINKER ERRORS ----
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2445: undefined reference to `std::string::size() const'
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2454: undefined reference to `std::string:perator[](unsigned long) const'
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2456: undefined reference to `std::string:perator[](unsigned long) const'
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2459: undefined reference to `std::string:perator[](unsigned long) const'
    cpu_cache_instructions.o: In function `main':
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:22: undefined reference to `rand'
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:32: undefined reference to `rand'
    cpu_cache_instructions.o: In function `std_Dev(float*, int, float)':
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:110: undefined reference to `pow'
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:113: undefined reference to `sqrt'
    cpu_cache_instructions.o: In function `__static_initialization_and_destruction_0':
    /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/iostream:77: undefined reference to `std::ios_base::Init::Init()'
    /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/iostream:77: undefined reference to `std::ios_base::Init::~Init()'
    cpu_cache_instructions.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'

    C++ CODE

    Code:
    #include <iostream>
    #include <math.h>
    #include <cstdlib>
    
    using namespace std;
    
    //Display functions
    float calculateMean(float [], int);
    float std_Dev(float [], int, float);
    
    //enter main function
    int main()
    {
    	//Declare variables
    	int count, greater = 0, greater_eq = 0, less = 0, less_eq = 0, eq = 0, nt_eq = 0;
        float Nums[100], old_STDDev, new_STDDev;
    	float score, totalScores = 0.0, mean = 0.0, STDDev;
    
    	//ask user how many scores user wishes to enter
    	////cout << "How many scores do you wish to enter? ";
    	
    	count = rand();
    	old_STDDev = 0.0;
    	
    
    	//for loop statement to enter all the scores from the amount the user wishes to enter
    	for (int i = 1; i <= 9; i++) {
    		
    		for (int num = 1; num <= 20; num++)
    		{
    		
    			Nums[num] = rand();
    			//cout << "The Random Number is " << Nums[num] << ": \n";
    		}
    
    		//Function to calculateMean()
    		mean = calculateMean(Nums, 20);
    		
    		STDDev = std_Dev(Nums,20,mean);
    		
    		new_STDDev = STDDev;
    		
    		if ( new_STDDev > old_STDDev ) {
    			
    			greater++;
    		}
    		
    		if ( new_STDDev < old_STDDev ) {
    			
    			less++;
    		}
    		
    		if ( new_STDDev == old_STDDev ) {
    			
    			eq++;
    		}
    		
    		if ( new_STDDev >= old_STDDev ) {
    			
    			greater_eq++;
    		}
    		
    		if ( new_STDDev <= old_STDDev ) {
    			
    			less_eq++;
    		}
    		
    		if ( new_STDDev != old_STDDev ) {
    			
    			nt_eq++;
    		}
    			
    		old_STDDev = new_STDDev;
    		
    	}
    	
    	//cout <<"The Standard Deviation comparason was " << greater_eq <<" "<< less <<" "<< less_eq <<" "<< eq <<" "<< nt_eq <<" \n " << endl;
    
    	//return a value
    	return 0;
    }
    
    //calculate Mean function
    float calculateMean(float Nums[], int inSize)
    {
    	//Declare variables
    	float mean, sum, Standard_Deviation;
    
    	//Calculate the mean
    	
    	for ( int i = 0; i < inSize; i++ ){
    		sum += Nums[i];
    	}
        mean = sum / inSize;
    
    	//Display the mean
    	//cout << "The mean is " << mean << "\n " << endl;
    	
    	return mean;
    	
    
    }
    
    //calculateStandardDeviation() function
    float std_Dev ( float Nums[], int inSize, float avg ){
    	
    	float sDev, sum2;
    
    	for ( int i = 0; i < inSize; i++ ){
    		sum2 += pow( Nums[i] - avg, 2 );
    	}
    
    	sDev = sqrt( sum2 / ( inSize - 1 ) );
    
    	//cout << "The STD Deviation is " << sDev << "\n " << endl;
    	
    	return sDev;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you have some kind of makefile for compiling and linking all your files?

    It looks like you have something like
    gcc $(OBJ)
    rather than
    g++ $(OBJ)

    You need to use g++ to invoke the linker, when trying to link C++ programs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    It looks like you are compiling with 'gcc' instead of 'g++'. You need to use the g++ compiler.

    It's all a big clue, but the dead giveaway is the last line:

    undefined reference to `__gxx_personality_v0'
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Hi

    When compiling and linking here are the commands that I use.

    sde-as -mips1 startup.s -o startup.o
    sde-g++ -c -g -mtune=r3k -mips1 fpchk_stddev.cpp -o cpu_cache_instructions.o
    sde-ld -mips1 -T ph.ld startup.o cpu_cache_instructions.o -o cpu_cache_instructions.elf
    sde-objdump --disassemble --line-numbers --sourcecpu_cache_instructions.elf > disassembly.list

    I belive I am using g++ but maybe the compiler is using g++ and the linker is not, is there an option to tell the linker to use g++?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You may also need to explicitly link in the math library, I believe the relevant command line parameter to add to the link line is something like -lm... others can confirm.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're invoking the linker directly (which isn't a problem in itself, but you need to know what you're doing).

    sde-g++ -mips1 -T ph.ld startup.o cpu_cache_instructions.o -o cpu_cache_instructions.elf
    will invoke the linker with ALL the correct libraries and flags for linking c++ programs.

    Try
    sde-g++ -v -mips1 -T ph.ld startup.o cpu_cache_instructions.o -o cpu_cache_instructions.elf
    for verbose more, to see all the other things it does.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    OK I added the -v option and I got the following.


    GNU ld version 2.15.94 mipssde-6.06.01-20070420
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2445: undefined reference to `std::string::size() const'
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2454: undefined reference to `std::string:perator[](unsigned long) const'
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2456: undefined reference to `std::string:perator[](unsigned long) const'
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2459: undefined reference to `std::string:perator[](unsigned long) const'
    cpu_cache_instructions.o: In function `main':
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:22: undefined reference to `rand'
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:32: undefined reference to `rand'
    cpu_cache_instructions.o: In function `std_Dev(float*, int, float)':
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:110: undefined reference to `pow'
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:113: undefined reference to `sqrt'
    cpu_cache_instructions.o: In function `__static_initialization_and_destruction_0':
    /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/iostream:77: undefined reference to `std::ios_base::Init::Init()'
    /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/iostream:77: undefined reference to `std::ios_base::Init::~Init()'
    cpu_cache_instructions.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    OK I added the -v command to the sde-g++ command and got the following


    Code:
    Reading specs from /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/specs
    Configured with: /var/tmp/releasetool.tmp/bank-20070420-1231/B-i386-linux/toolchain/mipssde-6.06.01/configure --target=sde --prefix=/usr/local/sde6 --enable-languages=c,c++ --without-newlib --disable-shared --disable-nls --disable-tui --disable-multilib
    Thread model: mipssde
    gcc version 3.4.4 mipssde-6.06.01-20070420
     /exports/Simulations/opt/MIPS/bin/../libexec/gcc/sde/3.4.4/cc1plus -quiet -v -iprefix /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/ fpchk_stddev.cpp -quiet -dumpbase fpchk_stddev.cpp -mtune=r3k -mips1 -auxbase-strip cpu_cache_instructions.o -g -version -o /tmp/ccR2dASZ.s
    ignoring nonexistent directory "/usr/local/sde6/include/c++/3.4.4"
    ignoring nonexistent directory "/usr/local/sde6/include/c++/3.4.4/sde"
    ignoring nonexistent directory "/usr/local/sde6/include/c++/3.4.4/backward"
    ignoring nonexistent directory "/usr/local/sde6/lib/gcc/sde/3.4.4/include"
    ignoring nonexistent directory "/var/tmp/releasetool.tmp/bank-20070420-1231/B-i386-linux/toolchain-build/sde/include"
    ignoring nonexistent directory "/usr/local/sde6/sde/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4
     /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/sde
     /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/backward
     /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/include
     /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../sde/include
    End of search list.
    GNU C++ version 3.4.4 mipssde-6.06.01-20070420 (sde)
            compiled by GNU C version 3.3.
    GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
     /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../sde/bin/as -EB -mips1 -g -no-mdebug -32 -mtune=r3k -v -o cpu_cache_instructions.o /tmp/ccR2dASZ.s
    GNU assembler version 2.15.94 (sde) using BFD version 2.15.94 mipssde-6.06.01-20070420

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And is it working now?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    No I get the same linker errors.


    Code:
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2445: undefined reference to `std::string::size() const'
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2454: undefined reference to `std::string::operator[](unsigned long) const'
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2456: undefined reference to `std::string::operator[](unsigned long) const'
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/bits/locale_facets.tcc:2459: undefined reference to `std::string::operator[](unsigned long) const'
    cpu_cache_instructions.o: In function `main':
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:22: undefined reference to `rand'
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:32: undefined reference to `rand'
    cpu_cache_instructions.o: In function `std_Dev(float*, int, float)':
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:110: undefined reference to `pow'
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:113: undefined reference to `sqrt'
    cpu_cache_instructions.o: In function `__static_initialization_and_destruction_0':
    /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/iostream:77: undefined reference to `std::ios_base::Init::Init()'
    /exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/iostream:77: undefined reference to `std::ios_base::Init::~Init()'
    cpu_cache_instructions.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    OK I changed the librarys to just <cmath> and <cstdlib> and the errors I have now are just related to the functions rand and sqrt. Any ideas?

    Code:
    cpu_cache_instructions.o: In function `main':
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:25: undefined reference to `rand'
    /home/vtwb73/usbdisk/Quik_Silva_Upc_FPII/test_bench/FP_Tests/fpchk_stddev.cpp:35: undefined reference to `rand'
    cpu_cache_instructions.o:/exports/Simulations/opt/MIPS/bin/../lib/gcc/sde/3.4.4/../../../../include/c++/3.4.4/cmath:397: undefined reference to `sqrtf'
    cpu_cache_instructions.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You have to add a command to the linker when you use the standard math library, if only when you invoke the linker yourself. The command is -lm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Error: expected identifier or ‘(’ before ‘{’ token
    By jpcanaverde in forum C Programming
    Replies: 66
    Last Post: 06-08-2010, 12:53 PM
  3. extern defined functions
    By williamsonj in forum C Programming
    Replies: 6
    Last Post: 04-23-2010, 11:05 PM
  4. linker error undefined reference to
    By BJtoVisualcC++ in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2007, 11:40 AM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM