VisualStudio2019 WSL2 g++で
#####std::threadを使ったところ
- /home/aruru_weed/projects/PipeTest/obj/x64/Release/main.o: in function `main':PipeTest C:\usr\bin\ld 1
- Id returned 1 exit status
- undefined reference to 'pthread_create'
threadを使って
pipeで接続したアプリケーションの標準出力を取得(?)したいです
該当のソースコード
C++
1#include "popen2.h" 2 3#include <iostream> 4#include <pthread.h> 5#include <thread> 6 7int main(int argc, char* argv[]) { 8 int Pipe_r = fileno(stdin); 9 int Pipe_w = fileno(stdout); 10 11 chdir("/mnt/d/linux_bin/"); 12 popen2(Pipe_r, Pipe_w); 13 14 std::string str; 15 16 std::thread th([Pipe_r, &str]() { 17 while (str != "end") 18 { 19 char buf[256] = { NULL }; 20 int size = read(Pipe_r, buf, 255); 21 if (size == -1) { 22 return 1; 23 } 24 if (buf[0] != '\000') { 25 printf("pipe:"); 26 fwrite(buf, 1, size, stdout); 27 printf("\n"); 28 } 29 } 30 }); 31 do { 32 std::cin >> str; 33 write(Pipe_w, str.c_str(), str.length()); 34 write(Pipe_w, "\n", 1); 35 } while (str != "end"); 36 th.join(); 37}
試したこと
"pthread.h"のinclude
プロパティ>C/C++>コマンドライン>追加のオプションに"-pthread"や"-lpthread"を追加
プロパティ>リンカー>入力>追加の依存ファイルにpthreadの追加(Error出ましたw)
回答1件
あなたの回答
tips
プレビュー