2012-10-20 92 views
2

所以我有3個文件:main.cpp,sort.h和sort.cpp。在這裏,他們是:我在VS12中編寫代碼,它不會在Linux上編譯

sort.h:

#include <iostream> 
class sorter 
{ 
    public: 
     void bubble(int*, int); 
     void selection(int*, int); 
     void insertion(int*, int); 
     void bubble(int*, int, bool); 
     void selection(int*, int, bool); 
     void insertion(int*, int, bool); 
     void print_arr(int*, int); 
}; 

sort.cpp:

#include "sort.h" 
void sorter::bubble(int* arr, int size) 
{ 
    this->bubble(arr, size, false); 
} 
void sorter::bubble(int* arr, int size, bool verbose) 
{ 
    ... 
} 

void sorter::selection(int* arr, int size) 
{ 
    this->selection(arr, size, false); 
} 
void sorter::selection(int* arr, int size, bool verbose) 
{ 
    ... 
} 

void sorter::insertion(int* arr, int size) 
{ 
    this->insertion(arr, size, false); 
} 
void sorter::insertion(int* arr, int size, bool verbose) 
{ 

} 

void sorter::print_arr(int* arr, int size) 
{ 
    ... 
} 

的main.cpp

// Standard C++ lib 
#include <iostream> 
#include <string> 
#include <sstream> 
#include <stdio.h> 

// Mina classes etc. 
#include "sort.h" 

int main() 
{ 
    system("title Uppgift 4, sorteringsalgoritmer. © Axel Latvala 2012"); 
    bool machineinput = false; 
    int size = 0; 
    int* pArr; 
    std::string input = ""; 
    bool verbose = false; 
    std::stringstream inputStream(""); 

    while(true) 
    { 
     int choice = -1; 
     std::cout << "Vill du köra i verbose mode? (1 = ja, 0 = nej): "; 
     getline(std::cin, input); 

     inputStream.str(input); 
     if(inputStream >> choice) 
     { 
      if(choice == 1) 
      { 
       verbose = true; 
        break; 
       } 
      else if(choice == 0) 
     { 
      verbose = false; 
      break; 
     } 
     std::cout << "Välj antingen 1 eller 0: "; 
    } 
    else 
    { 
     std::cout << "Välj antingen 1 eller 0: "; 
    } 
} 
system("cls"); 
std::cout << "Hur många element vill du mata in?\n"; 
while(true) 
{ 
    std::cout << "Antal element(eller 0 för talföljd): "; 
    getline(std::cin, input); 
    inputStream.str(""); 
    inputStream.clear(); 
    inputStream.str(input); 
    // String -> Int 
    if(inputStream >> size) 
    { 
     if(size == 0) 
     { 
      machineinput = true; 
      std::cout << "Du valde 0, vi genererar en talföljd. Hur många element?\n"; 
      std::cout << "Antal element:"; 
      int n = 0; 
      while(true) 
      { 
       getline(std::cin, input); 
       inputStream.str(""); 
       inputStream.clear(); 
       inputStream.str(input); 
       if(inputStream >> n) 
       { 
        if(n > 0) 
        { 
         break; 
        } 
        else 
        { 
         std::cout << "Antal element måste vara > 0.\n:"; 
        } 
        std::cout << "Antal element måste vara integer.\n:"; 
       } 
      } 
      size = n; 
      pArr = new int[size]; 
      std::cout << "1) Slumpmässig\n"; 
      std::cout << "2) n, n-1, n-2 ... n-(n-1)\n"; 
      std::cout << "3) n+1, n+2 ... n+(n-1)\n"; 
      std::cout << "Val: "; 
      bool ready = false; 
      while(!ready) 
      { 
       ready = true; 
       getline(std::cin, input); 
       inputStream.str(""); 
       inputStream.clear(); 
       inputStream.str(input); 
       int x = -1; 
       if(inputStream >> x) 
       { 
        switch(x) 
        { 
        case 1: 
         for(int x = 0;x<=n-1;x++) 
         { 
          pArr[x] = rand()+1; 
         } 
         break; 
        case 2: 
         for(int x = 0;x<=n-1;x++) 
         { 
          pArr[x] = n-x; 
         } 
         break; 
        case 3: 
         for(int x = 0;x<=n-1;x++) 
         { 
          pArr[x] = x+1; 
         } 
         break; 
        default: 
         std::cout << "Välj ett alternativ bland alternativen 1-3.\n:"; 
         ready = false; 
         break; 
        } 
       } 
       else 
       { 
        ready = false; 
        std::cout << "Välj ett alternativ bland alternativen 1-3.\n:"; 
       } 
      } 
     } 
     else 
     { 
      pArr = new int[size]; 
      input = ""; 
      system("cls"); 
      std::cout << "Antal element: " << size << ", godkännt.\n"; 
     } 
     break; 
    } 
    else 
    { 
     std::cout << "\"" << input << "\" duger inte som antal element. Försök igen.\n"; 
    } 
} 
if(!machineinput) 
{ 
    std::cout << "Var god och mata in elementen när de frågas efter.\n"; 
    for(int i=0;i<=size-1;i++) 
    { 
     while(true) 
     { 
      std::cout << "Mata in element nummer " << i+1 << ": "; 
      getline(std::cin, input); 

      std::stringstream inputStream(input); 
      if(inputStream >> pArr[i]) 
      { 
       std::cout << "Element " << i+1 << " = " << pArr[i] << "\n"; 
       break; 
      } 
      else 
      { 
       std::cout << "Elementet måste vara av typ integer.\n"; 
      } 
     } 
    } 
} 
system("cls"); 
sorter Sort; 
std::cout << "Start: "; 
Sort.print_arr(pArr, size); 
std::cout << "\n"; 
int* origArr; 
origArr = new int[size]; 
for(int x = 0;x<=size-1;x++) 
{ 
    origArr[x] = pArr[x]; 
} 
std::cout << "Du har att välja mellan 3st sortetingsalgoritmer, vilka som är följande:\n"; 
std::cout << "1) Bubble\n"; 
std::cout << "2) Selection\n"; 
std::cout << "3) Insertion\n"; 

while(true) 
{ 
    int ready = 0; 
    int choice = -1; 
    std::cout << "Välj sorteringsalgoritm: "; 
    getline(std::cin, input); 
    std::stringstream inputStream(input); 

    if(inputStream >> choice) 
    { 
     sorter* sortmachine = new sorter; 
     ready = 1; 
     std::cout << "Valde alternativ " << choice; 
     std::stringstream tmptitle; 
     char buffer[70]; 
     switch (choice) 
     { 
     case 1: 
      // Bubble 
      std::cout << ", bubble.\n\n"; 
      sprintf_s(buffer, "title Bubble sortering, %d element. © Axel Latvala\n", size); 
      system(buffer); 
      sortmachine->bubble(pArr, size, verbose); 
      break; 
     case 2: 
      // Selection 
      sprintf_s(buffer, "title Selection sortering, %d element. © Axel Latvala\n", size); 
      system(buffer); 
      std::cout << ", selection.\n\n"; 
      sortmachine->selection(pArr, size, verbose); 
      break; 
     case 3: 
      // Insertion 
      sprintf_s(buffer, "title Insertion sortering, %d element. © Axel Latvala\n", size); 
      system(buffer); 
      std::cout << ", insertion.\n\n"; 
      sortmachine->insertion(pArr, size, verbose); 
      break; 
     default: 
      ready = 0; 
      std::cout << ", okännt alternativ. Använd alternativen 1-3.\n\n"; 
      break; 
     } 
     if(ready == 1) 
     { 
      break; 
     } 
     delete sortmachine; 
     sortmachine = 0; 
    } 
    else 
    { 
     std::cout << "\"" << input << "\" är ett okännt alternativ. Använd alternativen 1-3.\n"; 
    } 
} 
std::cout << "Resultat: "; 
Sort.print_arr(pArr, size); 
std::cout << "\n"; 
std::cout << "Tryck enter för att sluta programmet.\n© Axel Latvala 2012\n"; 
getline(std::cin, input); 
return 0; 
} 

編譯和窗戶,但在linux上我運行良好得到這些錯誤:

[[email protected]: ~/cpp]$ c++ -I /home/akke/cpp main.cpp 
/tmp/ccJsJR5a.o: In function `main': 
main.cpp:(.text+0x9bc): undefined reference to `sorter::print_arr(int*, int)' 
main.cpp:(.text+0xbcc): undefined reference to `sorter::bubble(int*, int, bool)' 
main.cpp:(.text+0xc2b): undefined reference to `sorter::selection(int*, int, bool)' 
main.cpp:(.text+0xc87): undefined reference to `sorter::insertion(int*, int, bool)' 
main.cpp:(.text+0xd93): undefined reference to `sorter::print_arr(int*, int)' 
collect2: ld returned 1 exit status 

這是我第一次在linux上編譯,我做錯了什麼?

+3

您必須編譯都「的main.cpp」和「sort.cpp」。 – Joe

+0

我該怎麼做..? –

+1

快速和骯髒:''C++ -I/home/akke/cpp main.cpp sort.cpp' – Joe

回答

5

您的編譯命令不正確,您不包括sort.cpp。嘗試:

c++ -I /home/akke/cpp main.cpp sort.cpp 

展望未來,你可能想創建一個簡單的make文件,這應該與gmake這是常見的大多數Linux/UNIX機器上工作

INCLUDES= 
CC=/usr/bin/c++ 
CPPFLAGS= 
LIBS= 
DBG=-g 

.PHONY: all 

all: sorter 

sorter: main.o sort.o 
    $(CC) $+ -o sorter 

main.o: main.cpp main.h sort.h 
    $(CC) -c $(DBG) $(INCLUDES) $(CPPFLAGS) $+ 

sort.o: sort.cpp sort.h 
    $(CC) -c $(DBG) $(INCLUDES) $(CPPFLAGS) $+ 

然後你就可以(未經測試!)每次更改cpp文件時只需鍵入make,並且不必像上面的all-in-one命令那樣重新編譯每個文件。

3

另外,編譯成目標文件和鏈接之後他們:

c++ -c -I /home/akke/cpp main.cpp -o main.o 
c++ -c -I /home/akke/cpp sort.cpp -o sort.o 
c++ main.o sort.o -o programm 
+1

+1替代方式! –

+1

+1這實際上是'make'的做法,它避免瞭如果你只編輯一個源文件就編譯每個源文件。手動操作只是有點痛苦。 – Benj