我用VS12编写了代码,但它无法在Linux上编译
I wrote code in VS12 and it won't compile on Linux
所以我有3个文件:main.cpp,sort.h和sort.cpp。他们在这里:
排序.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);
};
排序.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)
{
...
}
主.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ässign";
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) Bubblen";
std::cout << "2) Selectionn";
std::cout << "3) Insertionn";
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.nn";
sprintf_s(buffer, "title Bubble sortering, %d element. © Axel Latvalan", size);
system(buffer);
sortmachine->bubble(pArr, size, verbose);
break;
case 2:
// Selection
sprintf_s(buffer, "title Selection sortering, %d element. © Axel Latvalan", size);
system(buffer);
std::cout << ", selection.nn";
sortmachine->selection(pArr, size, verbose);
break;
case 3:
// Insertion
sprintf_s(buffer, "title Insertion sortering, %d element. © Axel Latvalan", size);
system(buffer);
std::cout << ", insertion.nn";
sortmachine->insertion(pArr, size, verbose);
break;
default:
ready = 0;
std::cout << ", okännt alternativ. Använd alternativen 1-3.nn";
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 2012n";
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上编译,我做错了什么?
您的编译命令不正确,您没有包含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
,它就不必像使用上面的多合一命令那样重新编译每个文件。
或者,编译为目标文件并在之后链接它们:
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
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 如何从Linux编译到SDL2应用程序到Windows?
- 使用 MINGW 和 CPLEX 库链接从 Linux 编译 Windows 可执行文件
- 如何在Visual Studio中添加Linux编译到Cmake Project
- Linux 编译 - 冲突的库
- Linux C++编译错误
- Linux 编译错误 - 无法将类型"字符串"转换为类型"字符串&&"
- 当我在Windows上运行它时,为什么我的Linux编译二进制不起作用
- 使用Linux g++编译带有附加库的c++程序
- 如何在Windows中为Unix/Linux编译C++应用程序
- c++ Linux 编译错误
- 使用链接linux编译GLFW应用程序问题
- C++新手的 Linux 编译
- Linux编译错误-GCC 4.3.4-模板参数列表太少
- Linux g++ 编译错误:错误:预期的"",""或"..."在"||"标记之前
- Linux g++编译错误:/usr/bin/ld:找不到-l/usr/local/include
- Linux 编译"long distance"兄弟姐妹之间的dynamic_cast失败
- 为较旧的Linux编译c++ 11程序
- 如何安装适用于Mingw Linux编译环境的BerkeleyDB
- Windows Linux c++ 编译问题:"no matching function for call"