2015-07-10 23 views
3

所以我剛開始學習C++,我想在Linux上測試我的第一個程序。這裏是我的程序:如何在Windows PC上導出C++項目並在Linux上運行它?

#include <cstdlib> 
#include <iostream> 

#define ANSI_COLOR_RED  "\x1b[31m" 
#define ANSI_COLOR_GREEN "\x1b[32m" 
#define ANSI_COLOR_YELLOW "\x1b[33m" 
#define ANSI_COLOR_BLUE "\x1b[34m" 
#define ANSI_COLOR_MAGENTA "\x1b[35m" 
#define ANSI_COLOR_CYAN "\x1b[36m" 
#define ANSI_COLOR_RESET "\x1b[0m" 

using namespace std; 



void main_header() 
{ 
    printf (ANSI_COLOR_GREEN "+---------------+---------------¦ " ANSI_COLOR_RESET "\n"); 
    printf (ANSI_COLOR_GREEN "+---------" ANSI_COLOR_RED " THE GENERAL " "---------¦" ANSI_COLOR_RESET "\n"); 
    printf (ANSI_COLOR_GREEN "+---------------+---------------¦ " ANSI_COLOR_RESET "\n"); 
} 

int main() 
{ 
    main_header(); 
    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

我使用Bloodshhed來編譯程序。在完成程序後,我通過FTP上傳並執行dos2unix main.cpp。然後我嘗試運行該程序,但出現此錯誤:

./main.cpp: line 12: using: command not found 
./main.cpp: line 16: syntax error near unexpected token `(' 
./main.cpp: line 16: `void main_header()' 

我不知道爲什麼會出現此錯誤。有人有主意嗎?

+1

刪除'系統(「暫停」)',也 – AndyG

+0

你得到這個錯誤,當您嘗試運行?當然,這是一個編譯器錯誤。告訴我們更多關於你如何使用流血編譯它。 – AndyG

+2

嗯,你必須在目標機器上編譯它,然後才能執行它。您不能像運行腳本一樣簡單地運行.cpp文件。 –

回答

5

不是編譯和運行代碼,而是直接運行源代碼。

你想要做的,而不是什麼如下:

g++ main.cpp -o main 
./main 
+0

謝謝!有用! – NSPredator

2

你仍然需要在Linux上編譯程序!

在Linux上#是shell腳本註釋字符,所以第一行解釋爲shell命令是using namespace std

相關問題