我在VS12中编写代码,它不能在Linux上编译

时间:2012-10-20 14:40:40

标签: c++ windows linux visual-studio-2012

所以我有3个文件:main.cpp,sort.h和sort.cpp。他们在这里:

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;
}

在Windows上编译并运行正常但在linux上我得到了这些错误:

[akke@eresk: ~/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上编译,我做错了什么?

2 个答案:

答案 0 :(得分:5)

您的编译命令不正确,您不包括sort.cpp。尝试:

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

接下来,您可能想要创建一个简单的make文件,这应该适用于大多数Linux / UNIX计算机上常见的gmake(未经测试!)

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,而不必像上面的一体化命令那样重新编译每个文件。

答案 1 :(得分: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