popenの使用に関するエラー

このQ&Aのポイント
  • Microsoft Visual C++ 2008上でpopenを使用してdirコマンドと同じプログラムを作成しようとした際にエラーが発生しました。
  • ビルド時に関数 'popen' と 'pclose' の未解決外部シンボルエラーが発生しました。
  • 解決策を見つけることができず、質問を投げることにしました。
回答を見る
  • ベストアンサー

popen・pcloseについて

popen・pcloseについて 以下のようにpopenを使ってdirコマンドを打った時と同じようなプログラムを Microsoft Visual C++ 2008上で作ったのですが #include <stdio.h> int main(void) { FILE *fp; char buff[64]; fp=popen("dir","r"); while(fgets(buff,sizeof(buff),fp)!=NULL){ printf("%s",buff); } pclose(fp); return 0; } これをビルドした所、以下のようなエラーが出てしまいました。 c:\users\yamaomoto\documents\visual studio 2008\projects\popen\popen\popen.c(8) : warning C4013: 関数 'popen' は定義されていません。int 型の値を返す外部関数と見なします。 c:\users\yamaomoto\documents\visual studio 2008\projects\popen\popen\popen.c(8) : warning C4047: '=' : 間接参照のレベルが 'FILE *' と 'int' で異なっています。 c:\users\yamaomoto\documents\visual studio 2008\projects\popen\popen\popen.c(14) : warning C4013: 関数 'pclose' は定義されていません。int 型の値を返す外部関数と見なします。 popen.obj : error LNK2019: 未解決の外部シンボル _pclose が関数 _main で参照されました。 popen.obj : error LNK2019: 未解決の外部シンボル _popen が関数 _main で参照されました。 C:\Users\Yamaomoto\Documents\Visual Studio 2008\Projects\popen\Debug\popen.exe : fatal error LNK1120: 外部参照 2 が未解決です。 色々と試してみたのですが解決に至りませんでしたのでよろしければ、 教えて頂けませんでしょうか?

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

VC++の場合は関数名を  _popen  _pclose というように前に「_」を付ければ使えます。

yamaomoto
質問者

補足

ありがとうございました。ビルド出来ました。

その他の回答 (1)

  • Fllisy
  • ベストアンサー率27% (15/54)
回答No.2

popenとかは本来UNIX系OS専用関数ですが、それは大丈夫ですか? Windowsでやる場合には関数の前に_を付ける必要があります。 #include <stdio.h> int main(void) { FILE *fp; char buff[64]; fp=_popen("dir","r"); while(fgets(buff,sizeof(buff),fp)!=NULL){ printf("%s",buff); } _pclose(fp); return 0; } 多分コレで通るかと。

yamaomoto
質問者

補足

>>popenとかは本来UNIX系OS専用関数ですが、それは大丈夫ですか? 全く大丈夫じゃありませんでした。 関数名は完全にUNIXでもVC++でも同じだと思っていました… まだ、VC++始めて浅くてその辺りの理解が足りてませんでした。 そのあたり、UNIXとVC++で関数名の違いが載っているサイトとかご存知でしょうか?

関連するQ&A

  • UNIX関数?(popen)について

    Cプログラムでpopen関数?を使用しコマンド(rcp)を実行するプログ ラムを造ったのですが、当分が動作していたのですが、何のタイミングか分からないのですが、失敗する時が有ります。一度失敗するとそれ以降はずーと失敗します 。どのような原因が考えられるのでしょうか? ちなみにプログラムの一部をとりあえずのせておきます。(ファイル名及び相手先 のディレクトリ名は現状存在します) 又、system関数とpopen関数の大きな違いは何でしょうか? ご教示お願い致します。 (cプログラムの一部) char buff[256] ; char cmnd[256] ; /* コマンド文字列 */ int fp_rtc ; memset( buff, NULL, sizeof(buff) ) ; memset( cmnd, NULL, sizeof(cmnd) ) ; strcpy( buff, argv[1] ) ; sprintf(cmnd, RCP_FMT , buff, RCP_DIR, buff ) ; printf( "rcp cmnd ( %s )\n", cmnd ) ; fp = popen(cmnd, "r") ; fflush(stdout) ; fp_rtc = pclose(fp) ; if ( fp_rtc != 0 ){ printf( "pclose err [ rtc:%d ] \n", fp_rtc ) ; }

  • popenについて

    現在、UNIX同士の通信で以下のような処理をしています。 fp = popen( "rsh -l user host /xxx/yyy/zzzzz > aaa.txt", "w" );      : ret = fprintf( fp, "%s\n", buff );      : ret = fprintf( fp, "%s\n", buff ); pclose( fp ); 今回、こちらをWindowsPC(XP,C/C++で開発)に変更しようとしています。 相手は変更せずに、同じことをするには、どういう方法があるのでしょうか?

  • リンクの問題

    リンクしています... MSVCRTD.lib(crtexew.obj) : error LNK2019: 未解決の外部シンボル _WinMain@16 が関数 ___tmainCRTStartup で参照されました。 C:\Documents and Settings\hiro\My Documents\Visual Studio 2008\Projects\2maikyori\Debug\2maikyori.exe : fatal error LNK1120: 外部参照 1 が未解決です。  というerrorが出てくるのですが、どうすればリンクできるようになるのでしょうか?  ご教授お願いします。

  • Visial C++におけるプログラミング

    Visial C++ 2008を使用したプログラミングについてです。 学校の課題をやるためやむなくVisial C++2008を使用していて 学校では実行が出来たのに家では出来ませんでした。 ソースファイルは以下のとおりです。 #include <stdio.h> #include <stdlib.h> #include <time.h> /*構造体宣言*/ struct Students{ int N; int A; int B; int C; }; int main(void){ struct Students std[50]; /*構造体型配列*/ int i,N,A,B,C,num,scannum; srand((unsigned)time(NULL)); /*乱数の初期化*/ FILE *file; /*ファイルのポインタを用意*/ file=fopen("Data.txt","r"); /*Dataファイルの読み込み*/ /*ファイルのオープンチェック*/ if(file==NULL){ fprintf(stderr,"cannnot open file 'Data.txt'\n"); exit(1); } /*Studentsにデータを格納*/ for(i=0;i<=49;i++){ fscanf(file,"%d%d%d",&N,&A,&B); std[i].N=N; std[i].A=A; std[i].B=B; std[i].C=70+(rand()/(RAND_MAX+1.0)*31); /*表示*/ printf("学籍番号:%d.",std[i].N); printf("科目A:%d.\n",std[i].A); printf("科目B:%d.\n",std[i].B); printf("科目C:%d.\n",std[i].C); } fclose(file); return 0; } エラーとしては 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(21) : error C2275: 'FILE' : この型は演算子として使用できません 1> c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(69) : 'FILE' の宣言を確認してください。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(21) : error C2065: 'file' : 定義されていない識別子です。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(23) : error C2065: 'file' : 定義されていない識別子です。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(23) : warning C4047: '=' : 間接参照のレベルが 'int' と 'FILE *' で異なっています。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(26) : error C2065: 'file' : 定義されていない識別子です。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(26) : warning C4047: '==' : 間接参照のレベルが 'int' と 'void *' で異なっています。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(32) : error C2065: 'file' : 定義されていない識別子です。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(32) : warning C4047: '関数' : 間接参照のレベルが 'FILE *' と 'int' で異なっています。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(32) : warning C4024: 'fscanf' : の型が 1 の仮引数および実引数と異なります。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(36) : warning C4244: '=' : 'double' から 'int' への変換です。データが失われる可能性があります。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(45) : error C2065: 'file' : 定義されていない識別子です。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(45) : warning C4047: '関数' : 間接参照のレベルが 'FILE *' と 'int' で異なっています。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(45) : warning C4024: 'fclose' : の型が 1 の仮引数および実引数と異なります。 1>ビルドログは "file://c:\Documents and Settings\devil\My Documents\Visual Studio 2008\Projects\テスト\テスト\Debug\BuildLog.htm" に保存されました。 と表示されます。FILEの宣言をしているのになんで確認してください と出るのでしょうか。。。。解決にご協力お願いします。m(__)m

  • C++ memory.dllサンプルが実行できない

    http://www.vector.co.jp/soft/win95/prog/se358509.html のmemory.dllを用いてプロセスメモリを弄るソフトを作ってみようと思い まず同梱されているサンプルをVisual Studio 2010 Professionalでコンパイルしてみようとしたのですが (サンプルの場所はダウンロードしたZIP内の\サンプル集\C言語\ のsample.cpp Memory.hです。) 1>sample.obj : error LNK2019: 未解決の外部シンボル _WriteMemory@16 が関数 "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) で参照されました。 1>sample.obj : error LNK2019: 未解決の外部シンボル _ReadMemory@16 が関数 "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) で参照されました。 1>C:\Users\hogehoge\Documents\Visual Studio 2010\Projects\testtesttest\Debug\testtesttest.exe : fatal error LNK1120: 外部参照 2 が未解決です。 と出力され、コンパイルされません。 何か問題があると思い、Google検索で色々調べたのですが分かりません・・・ 宜しければご助言を頂きたいです。お願い致します。

  • CPLEX をCから使う方法について(環境設定)

    自分の環境 Microsoft Visual Studio 2010 Version 10.0.30319.1 RTMRel Microsoft .NET Framework Version 4.0.30319 RTMRel インストールされているバージョン: VC Express http://www.fl.ctrl.titech.ac.jp/paper/2004/Thesis/B_taka.pdf#search='CPXopenCPLEX()' 上記のサイトを見ながらCPLEXをCから呼び出してLP問題を解きたいのですが最初の環境設定のところでつまずいてしまいました。 プロジェクト→プロパティ→構成プロパティ→C/C++→追加のインクルードディレクトリ C:\ILOG\CPLEX_Studio_AcademicResearch122\cplex\include\ilcplex←cplex.hのある場所を指定 プロジェクト→プロパティ→構成プロパティ→リンカ→追加の依存ファイル→cplex122.libを指定 自分で一応このような設定はしました、しかしpathの指定の方法などがわかりません。 下記のように骨組みだけ組んでみたのですがエラーが起きてしまいます。 #include <cplex.h> #include <stdlib.h> #include <string.h> #include <stdio.h> int main () { CPXENVptr env=NULL; int status=0; env = CPXopenCPLEX(&status); } エラー 2 error LNK2028: 未解決のトークン (0A00001E) "extern "C" struct cpxenv * __stdcall CPXopenCPLEX(int *)" (?CPXopenCPLEX@@$$J14YGPAUcpxenv@@PAH@Z) が関数 "int __cdecl main(void)" (?main@@$$HYAHXZ) で参照されました。 c:\Users\msysstu16-1\documents\visual studio 2010\Projects\cplexcc\cplexcc\cplexc.obj エラー 3 error LNK2019: 未解決の外部シンボル "extern "C" struct cpxenv * __stdcall CPXopenCPLEX(int *)" (?CPXopenCPLEX@@$$J14YGPAUcpxenv@@PAH@Z) が関数 "int __cdecl main(void)" (?main@@$$HYAHXZ) で参照されました。 c:\Users\msysstu16-1\documents\visual studio 2010\Projects\cplexcc\cplexcc\cplexc.obj エラー 4 error LNK1120: 外部参照 2 が未解決です。 c:\users\msysstu16-1\documents\visual studio 2010\Projects\cplexcc\Debug\cplexcc.exe 1 今現在どのような問題が考えられるのか、また、どのような調査を行うべきか、ご教示頂きたいです。 よろしくお願い致します。

  • _popen() のエラー取得

    WindowsNT/2000で_popen()を利用してコマンドを実行しているのですが、その際のメッセージを取得したいのですが、通常のメッセージは取得出来るのですが、エラーメッセージが取得出来ません。どうすればよいのでしょうか? 現在のソースは下記のようです。 int main(int argc, char* argv[]) { char str[512],*ptr; FILE *fp; if((fp = _popen("dir b:","rt")) == NULL) { fprintf( stderr , "error!!!\n"); return -1; } while(1) { fgets(str,512,fp); if(feof(fp)) { break; } ptr = strchr(str,'\n'); if(ptr != NULL) { *ptr='\0'; } printf("%s\n",str); } _pclose(fp); return 0; }

  • 「猫でもわかるwindowsプログラミング」のサンプルのエラーです

     今日は、「猫でもわかるwindowsプログラミング」という本を買ってCを勉強している初心者です。  その中のサンプルプログラムを動かそうとしても、下のようなエラーが出て、動きません。  そのCDには、実行ファイル(EXE)も有るのですが、それは普通に立ち上がり、Windowが表示されます。  どのサンプルプログラムも全て同じエラーになります。  出版元には一応問い合わせていますが、回答がありません。  どこが原因でしょうか、エラーの意味もさっぱり分かりません。  実行環境は、Win XP Visual Studio 2008 です、宜しくお願いします。 =============================================================== 「エラー 1 error LNK2019: 未解決の外部シンボル _main が関数 ___tmainCRTStartup で参照されました。 MSVCRTD.lib sample01」 「エラー 2 fatal error LNK1120: 外部参照 1 が未解決です。 C:\Documents and Settings\My Documents\Visual Studio 2008\Projects\sample01\Debug\sample01.exe sample01」

  • c++によるエラー

    ここ(http://japan.maxim-ic.com/products/ibutton/software/1wire/wirekit.cfm)のサイトの、1-Wire Public Domain Kit + examples + all link files + assembly language files これをDLし、その中にあるThermoを実行したいのですが、エラーが出てしまいます。 ソースファイルに Thermodl.c , Thermoms.c ヘッダーファイルに findtype.h , ownet.h , thermo21.h を入れてあります。 これを実行すると、以下のエラーが出てしまいます。 1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hiraki\hiraki\thermodl.c(11): error C2143: 構文エラー : ')' が '*' の前にありません。 1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hiraki\hiraki\thermodl.c(11): error C2143: 構文エラー : '{' が '*' の前にありません。 1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hiraki\hiraki\thermodl.c(11): error C2059: 構文エラー : '型' 1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hiraki\hiraki\thermodl.c(11): error C2059: 構文エラー : ')' 1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hiraki\hiraki\thermodl.c(114): warning C4013: 関数 'PrintResults' は定義されていません。int 型の値を返す外部関数と見なします。 1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\hiraki\hiraki\thermodl.c(145): error C2371: 'PrintResults' : 再定義されています。異なる基本型です。 1> 1>ビルドに失敗しました。 visual studio2010の製品版を使っています。どのように修正すればよいでしょうか。 教えてください。

  • エラー 未解決の外部シンボル

    プログラム初心者です。 以下のエラーが出て解決方法がわからず困っております。ご存知の方宜しくお願いします。 エラー 1 error LNK2019: 未解決の外部シンボル __imp__ntohs@4 が関数 "void __cdecl packet_print(unsigned char *,struct pcap_pkthdr const *,unsigned char const *)" (?packet_print@@YAXPAEPBUpcap_pkthdr@@PBE@Z) で参照されました。 main.obj エラー 2 fatal error LNK1120: 外部参照 1 が未解決です。 C:\Documents and Settings\谷本\My Documents\Visual Studio 2005\Projects\Packetmoni_Base\Debug\Packetmoni_Base.exe