簡體   English   中英

使用c ++ 11配置eclipse

[英]Configure eclipse with c++11

首先,我已經檢查了不同的解決方案:

線程std :: system_error

線程std :: system_error II

用gcc編譯多線程

Bug gcc多線程

我的環境:

Ubuntu 1404 64位

gcc 4.8.2

用於C / C ++開發人員的Eclipse IDE

版本:Luna Service Release 1(4.4.1)Build id:20140925-1800

按照這些鏈接,我設法編譯並運行一個基本的線程程序(代碼來自SO中的一個鏈接,但無法再找到它。如果有人看到它,請編輯我的問題以添加引用)。

#include <iostream>
#include <thread>
#include <vector>

void func(int tid)
{
    std::cout << "Launched by thread " << tid << std::endl;
}

int main()
{
    std::vector<std::thread> th;

    int nr_threads = 10;
    for(int i = 0; i < nr_threads; ++i)
    {
        th.push_back(std::thread(func, i));
    }

    for(auto &t : th)
    {
        t.join();
    }

    return 0;
}

我用來編譯它的命令如下( 這個工作和輸出文件是可執行的):

g++ --std=c++11 -pthread test.cpp -o test.out

Launched by thread 1
Launched by thread 5
Launched by thread 3
Launched by thread 6
Launched by thread 4
Launched by thread 0
Launched by thread 7
Launched by thread 8
Launched by thread 9
Launched by thread 2

問題是當我嘗試設置我的eclipse項目時。 我可以編譯,但它不能產生有效的runnable輸出。

編譯日志:

12:09:45 **** Incremental Build of configuration Debug for project test ****
make all 
Building file: ../test.cpp
Invoking: GCC C++ Compiler
g++ --std=c++11 -pthread -D__cplusplus=201103L -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"test.d" -MT"test.d" -o "test.o" "../test.cpp"
Finished building: ../test.cpp

Building target: test
Invoking: GCC C++ Linker
g++  -o "test"  ./test.o   
Finished building target: test


12:09:46 Build Finished (took 780ms)

我正在改變構建器,方言的不同設置......正如他們在鏈接中所說的那樣試圖獲得我可以用來從終端編譯的相同命令。 但無法從eclipse創建有效的輸出文件。 它總是顯示此錯誤:

terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted

知道如何設置eclipse嗎?

更新:遵循@Dirk的指示我在虛擬機中進行了測試,它只是將pthread添加到鏈接器庫。

但對於我的初始設置,它仍然失敗。 在將C / C ++ Build設置G ++鏈接器命令修改為g++ --std=c++0x -pthread后,我使其工作g++ --std=c++0x -pthread

所以,我的第一個環境似乎很遺憾。

似乎缺少與pthread庫的鏈接。 在Build-> Settings-> Tool Settings-> GCC C ++ Linker-> Libraries下添加“pthread”。

這是我的構建日志所說的:

**** Build of configuration Debug for project testpthreads ****

make all 
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++0x -pthread -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp

Building target: testpthreads
Invoking: GCC C++ Linker
g++  -o "testpthreads"  ./main.o   -lpthread
Finished building target: testpthreads

您的代碼然后工作和輸出:

Launched by thread Launched by thread Launched by thread 1
Launched by thread 0
Launched by thread 2
Launched by thread 9
Launched by thread 3
Launched by thread 7
Launched by thread 6
4
Launched by thread 8
5

我得到了與沒有-lpthread相同的錯誤

我的鏈接器設置: linkersettings

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM