C言語でPostgreSQLをアクセスする方法とコンパイルエラーの解決方法

このQ&Aのポイント
  • C言語を使用してPostgreSQLにアクセスする方法と、コンパイルエラーの解決方法について教えてください。
  • PostgreSQL 8.4.8のサンプルプログラムをC言語でコンパイルしようとしていますが、undefined referenceエラーが発生します。
  • gccコマンドを使用してコンパイルしてもエラーが解消されません。どのようにすれば解決できるでしょうか?
回答を見る
  • ベストアンサー

PostgreSQLをC言語でアクセスしようと

PostgreSQL 8.4.8 文書にのっているサンプルプログラムの 例 30-1. libpq サンプルプログラム 1 #include <stdio.h> #include <stdlib.h> #include "libpq-fe.h" ・・・・・・・・・・・・・ をコピペしてコンパイルしたところ次のようなエラーがでました # gcc sample.c /tmp/cctZBolQ.o: In function `exit_nicely': sample.c:(.text+0xd): undefined reference to `PQfinish' /tmp/cctZBolQ.o: In function `main': sample.c:(.text+0x4a): undefined reference to `PQconnectdb' sample.c:(.text+0x5a): undefined reference to `PQstatus' sample.c:(.text+0x6a): undefined reference to `PQerrorMessage' sample.c:(.text+0xa5): undefined reference to `PQexec' sample.c:(.text+0xb5): undefined reference to `PQresultStatus' sample.c:(.text+0xc6): undefined reference to `PQerrorMessage' sample.c:(.text+0xed): undefined reference to `PQclear' sample.c:(.text+0x105): undefined reference to `PQclear' sample.c:(.text+0x119): undefined reference to `PQexec' sample.c:(.text+0x129): undefined reference to `PQresultStatus' sample.c:(.text+0x13a): undefined reference to `PQerrorMessage' sample.c:(.text+0x161): undefined reference to `PQclear' sample.c:(.text+0x179): undefined reference to `PQclear' sample.c:(.text+0x18d): undefined reference to `PQexec' sample.c:(.text+0x19d): undefined reference to `PQresultStatus' sample.c:(.text+0x1ae): undefined reference to `PQerrorMessage' sample.c:(.text+0x1d5): undefined reference to `PQclear' sample.c:(.text+0x1ed): undefined reference to `PQnfields' sample.c:(.text+0x20f): undefined reference to `PQfname' sample.c:(.text+0x26b): undefined reference to `PQgetvalue' sample.c:(.text+0x2a8): undefined reference to `PQntuples' sample.c:(.text+0x2ba): undefined reference to `PQclear' sample.c:(.text+0x2ce): undefined reference to `PQexec' sample.c:(.text+0x2de): undefined reference to `PQclear' sample.c:(.text+0x2f2): undefined reference to `PQexec' sample.c:(.text+0x302): undefined reference to `PQclear' sample.c:(.text+0x30e): undefined reference to `PQfinish' collect2: ld はステータス 1 で終了しました # # find /usr/include/ -name libpq*.h /usr/include/pgsql/internal/libpq-int.h /usr/include/pgsql/server/libpq/libpq-be.h /usr/include/pgsql/server/libpq/libpq.h /usr/include/pgsql/server/libpq/libpq-fs.h /usr/include/libpq/libpq-fs.h /usr/include/libpq-events.h /usr/include/libpq-fe.h # gcc -I/usr/include sample.c でもおなじです どうすればコンパイル出きるようになるでしょうか? よろしくお願いします

  • reiman
  • お礼率73% (794/1076)

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

  • ベストアンサー
  • Wr5
  • ベストアンサー率53% (2177/4070)
回答No.1

># gcc sample.c ># gcc -I/usr/include sample.c ライブラリの指定がないみたいですが……環境変数とかで指定しているのでしょうか? 「PostgreSQL libpq gcc」で検索して適当に見つかるページですが… http://www.fireproject.jp/feature/postgresql/programing_libpq/connect.html より… >これをコンパイルするには,ヘッダとライブラリの指定が必要である. >gcc -I/usr/include/postgresql/ -lpq connect.c ということで、-lpqも必要かと思われますがいかがでしょう?

reiman
質問者

お礼

ありがとうございます # gcc -lpq sample.c とすれば動きました

関連するQ&A

  • LinuxのgccのLIBパス? (類似質問)

    Linuxのgccで、LIBのパスを設定する方法が知りたいのですが、gccについて詳しい書籍やサイトがありましたら、教えてください。 例)gcc -I/usr/local/example/include test.c /tmp/ccOFpCBo.o(.text+0x26): In function `main': : undefined reference to `EXAMPLE_1' /tmp/ccOFpCBo.o(.text+0x37): In function `main': : undefined reference to `EXAMPLE_2' /tmp/ccOFpCBo.o(.text+0x48): In function `main': : undefined reference to `EXAMPLE_3' collect2: ld はステータス1で終了しました。 また、ldとはVC6++でいう LINKのことでしょうか?

  • コンパイル時のエラー

    UNIXでサーバー、クライアントのCプログラミングをテキストどおりに書いてコンパイルしたのですが、以下のエラーが出ます。gcc, ccでコンパイルすると以下のようになるのですが、これはどういうことなのでしょうか?何が原因なのでしょうか?よろしくお願いいたします。 gcc unix_server.c /var/tmp/ccvpay1f1.o: In function `main': /var/tmp/ccvpay1f1.o(.text+0x8c): undefined reference to `socket' /var/tmp/ccvpay1f1.o(.text+0x104): undefined reference to `unllnk' /var/tmp/ccvpay1f1.o(.text+0x118): undefined reference to `bind' /var/tmp/ccvpay1f1.o(.text+0x150): undefined reference to `listen' /var/tmp/ccvpay1f1.o(.text+0x1a8): undefined reference to `accept' cc unix_server.c ld: /usr/lib/cmplrs/cc/crtn.o: Error: Unresolved: rindex socket bzero bcopy unllnk bind listen accept

  • c++における文字出力

     c++初学者です(cは少しだけ知っています).  私が持っているc++の入門書には,「coutを使って文字を出力できる」旨書かれています.そこで,その本に載っているcoutを使ったサンプル(*1)を書いて実行してみたのですが,下のようなエラーが出ます. > /var/tmp/ccT163831.o: In function `main': > /var/tmp/ccT163831.o(.text+0x9): undefined reference to `cout' > /var/tmp/ccT163831.o(.text+0xe): undefined reference to `ostream::operator<<(char const *)'  本で使われているコンパイラはBorlandのもので,私が使っているものはgccです.  coutを使って出力する方法はBorland特有の(あるいはその他いくつかのコンパイラだけで使われている)方法で,より一般的にc++を勉強したい場合は他の参考書を使うべきなのでしょうか.それとも,私の使っているgcc(ver 2.7.2.3)が古すぎる,あるいは何らかの設定が間違っていて使えない,ということなのでしょうか.  どなたかご教授ください. (*1) #include <iostream.h> void main(void) { cout << "this is a test."; }

  • C言語のpow関数の不具合

    linux(ubuntu)の上でgccを使っています。 powの関数がおかしいです。 pow(10,-1)を計算させると正しく1e-1が出力されます。 #include <math.h> #include <stdio.h> int main(void){ double x = -1; printf("%f",pow(10,x)); return 0; } 上記のプログラムを走らせると、 In function "main"; *****.c:(.text+0x2d):undefined reference to 'pow' collect2: ldはステータス1で終了しました。 というエラーメッセージがでます。 なぜかpow関数の中に変数を入れるとプログラムが動きません。

  • C言語コンパイラーエラーについて教えてください。

    C言語でコンパイルができなくて困っています。 コンパイルをすると、 ----------------------------------------- test.c.20:undefined reference to `getcury' test.c:20: undefined reference to `getcurx' collect2: ld returned 1 exit status ----------------------------------------- と言うエラーが出てしまい、コンパイルが完了しません。 ソースは、「test.c」と言うファイルネームで、内容は ----------------------------------------- #include <stdio.h> #include <stdlib.h> #include <curses.h> int main (void) { int y = 0, x = 0; initscr(); getyx(curscr, y, x); printw("x = %d, y = %d", x, y); refresh(); getchar(); endwin(); return 0; } ----------------------------------------- です。 OSは、Windows7 Professional SP1 コンパイラーは、MinGW Ver5.1.6. で、コマンドは gcc -o test.exe test.c -lcurses です。 確かに、curses へはパスも通っており、リンクできているようなんですが、 エラーが出てしまいます。 どうすれば良いのかわからず、大変困っています。 だれかご存知の方、教えてください。 どうかよろしくお願い致します。

  • C言語の画像抽出

    C言語のプログラムをコンパイルした時に ”/tmp/cchzThVk.o: In function 'c_to_sh': 5.c:(.text+0x5b3): undefined reference to 'sqrt' 5.c:(.text+0x64e): undefined reference to 'atan2' collect2: Id は ステータス1で終了しました。” とメッセージが出るのですが、このメッセージが出たときはプログラムにミスがあるということなのでしょうか。教えてくださいお願いします。

  • C++の関数をfortranから呼びたい。(g++, g77)

    C++の関数をfortranから呼びたい。(g++, g77) いつもお世話になっています。 現在WindowsXPにMinGWというソフトを入れて C++のコンパイルはg++を Fortranのコンパイルはg77を 使用しています。 (いずれも動作は確認できています。) 現在、以下のようなソースを使用して fortranからC++関数を呼ぼうとしていますが できておりません。 ソース内容、コンパイル方法に問題があれば ご指摘頂きたくお願いします。 ■C++ソース #include <stdio.h> #include <iostream> extern "C" void funcHelloC_(); void funcHelloC_(void) { printf("hello C world!\n"); } ■fortranソース    write(*,*) "Hello Fortran World."    call funcHelloC()    end ※実際のソースの各文先頭には、半角スペース6つ分が  記述しています。 ■コンパイル手順 D:\C>g++ -c funcHelloC.cpp D:\C>g77 -c helloFortran.f D:\C>g77 helloFortran.o funcHelloC.o helloFortran.o(.text+0x51):helloFortran.f: undefined reference to `funchelloc_' funcHelloC.o(.text+0xd):funcHelloC.cpp: undefined reference to `_ZNKSs4sizeEv' funcHelloC.o(.text+0x60):funcHelloC.cpp: undefined reference to `_ZNKSsixEj' funcHelloC.o(.text+0x9e):funcHelloC.cpp: undefined reference to `_ZNKSsixEj' funcHelloC.o(.text+0xcc):funcHelloC.cpp: undefined reference to `_ZNKSsixEj' funcHelloC.o(.text+0x12f):funcHelloC.cpp: undefined reference to `_ZNSt8ios_base4InitC1Ev' funcHelloC.o(.text+0x14a):funcHelloC.cpp: undefined reference to `_ZNSt8ios_base4InitD1Ev'

  • sendmailのコンパイルエラーについて

    教えてください。>_< お願いします。 sendmail-8.12.2のインストールを試みたのですが、 エラーが返ってきます。 このエラーの意味、対処法などご存知の方 教えてください。 <エラー内容> domain.o: In function `getmxrr': domain.o(.text+0x25f): undefined reference to `__res_query' domain.o(.text+0x266): undefined reference to `__res_search' domain.o(.text+0x39b): undefined reference to `__dn_skipname' domain.o(.text+0x496): undefined reference to `__dn_expand' domain.o(.text+0x603): undefined reference to `__dn_expand' domain.o: In function `dns_getcanonname': domain.o(.text+0xf0c): undefined reference to `__res_querydomain' domain.o(.text+0x100f): undefined reference to `__dn_skipname' domain.o(.text+0x123f): undefined reference to `__dn_expand' domain.o(.text+0x128a): undefined reference to `__dn_expand' sm_resolve.o: In function `parse_dns_reply': sm_resolve.o(.text+0x17e): undefined reference to `__dn_expand' sm_resolve.o(.text+0x1fe): undefined reference to `__dn_expand' sm_resolve.o(.text+0x2e5): undefined reference to `__dn_expand' sm_resolve.o(.text+0x337): more undefined references to `__dn_expand' follow sm_resolve.o: In function `dns_lookup_int': sm_resolve.o(.text+0x612): undefined reference to `__res_search' collect2: ld returned 1 exit status make: *** [sendmail] エラー1 gccはバージョン2.96-81です。 OSはRedhatLinux7.1Jです。 よろしくお願いします。m(_)m

  • Fedora7で libpthread.aをリンクすると、コンパイルエラーになってしまいました

    はじめまして、 Fedora7でマルチスレッドのプログラムをビルドし、 libpthread.aをリンクしたところ、コンパイルエラーになってしまいました。 Fedora4~5では、起きなかったため、 libpthread.aのバージョン変更により、問題がおきたのではないかと思っております。 libpthreadのバージョンは2.6-4で [root@localhost]# rpm -qf /usr/lib/libpthread.a glibc-devel-2.6-4 glibc-develをダウングレードしようと思いましたが、 glibc-commonの依存関係が厳しく、forceしてもうまくいきませんでした。 makeのエラーは下記の内容で、main.cppにlibpthread.aをリンクしてビルドすると、 libpthread.a内で使ってる関数がundefinedでこけてしまいます。 ネットを検索して、pthreadのについて書かれているサイトを読みましたが、あまり見つからずどうして良いやらわからない状態です。 ttp://uyota.asablo.jp/blog/2007/01/27/1142762 Fedora7でlibpthread.aをリンクし、 undefinedを回避して、うまくビルドする方法をご存知でしたら、お教えください。 ---------------- エラー内容 [root@localhost test]# make make clean make[1]: ディレクトリ `/src/test' に入ります rm Main.o removed `Main.o' make[1]: ディレクトリ `/src/test' から出ます make test make[1]: ディレクトリ `/src/test' に入ります g++ -c -o Main.o Main.cpp g++ -o test Main.o /usr/lib/libpthread.a -lpthread /usr/lib/libpthread.a(pthread_create.o): In function `pthread_create': (.text+0xca3): undefined reference to `_dl_stack_flags' /usr/lib/libpthread.a(pthread_create.o): In function `pthread_create': (.text+0x1126): undefined reference to `_dl_stack_flags' /usr/lib/libpthread.a(ptw-accept.o): In function `accept': (.text+0x23): undefined reference to `__syscall_error' /usr/lib/libpthread.a(ptw-accept.o): In function `accept': (.text+0x52): undefined reference to `__syscall_error' /usr/lib/libpthread.a(ptw-send.o): In function `__send': (.text+0x23): undefined reference to `__syscall_error' /usr/lib/libpthread.a(ptw-send.o): In function `__send': (.text+0x52): undefined reference to `__syscall_error' /usr/lib/libpthread.a(ptw-pause.o): In function `__pause_nocancel': (.text+0x18): undefined reference to `__syscall_error' /usr/lib/libpthread.a(ptw-pause.o):(.text+0x3b): more undefined references to `__syscall_error' follow /usr/lib/libpthread.a(init.o): In function `__pthread_initialize_minimal': (.text+0x16c): undefined reference to `__libc_setup_tls' /usr/lib/libpthread.a(init.o): In function `__pthread_initialize_minimal': (.text+0x328): undefined reference to `_dl_init_static_tls' /usr/lib/libpthread.a(init.o): In function `__pthread_initialize_minimal': (.text+0x34d): undefined reference to `_dl_wait_lookup_done' /usr/lib/libpthread.a(ptw-read.o): In function `__read_nocancel': (.text+0x26): undefined reference to `__syscall_error' /usr/lib/libpthread.a(ptw-read.o): In function `__read_nocancel': (.text+0x56): undefined reference to `__syscall_error' /usr/lib/libpthread.a(ptw-open.o): In function `__open_nocancel': (.text+0x26): undefined reference to `__syscall_error' /usr/lib/libpthread.a(ptw-open.o): In function `__open_nocancel': (.text+0x56): undefined reference to `__syscall_error' /usr/lib/libpthread.a(sigaction.o): In function `__libc_sigaction': (.text+0x60): undefined reference to `_dl_sysinfo_dso' /usr/lib/libpthread.a(sigaction.o): In function `sigaction': (.text+0x19a): undefined reference to `_dl_sysinfo_dso' collect2: ld はステータス 1 で終了しました make[1]: *** [test] エラー 1 make[1]: ディレクトリ `/src/test' から出ます make: *** [build] エラー 2 -- [root@localhost]# cat /etc/ld.so.conf /usr/lib include ld.so.conf.d/*.conf

  • c言語 コンパイルエラー

    以下webページに載っているC言語のプログラムをコンパイルした時に ”/tmp/cchzThVk.o: In function 'c_to_sh': 5.c:(.text+0x5b3): undefined reference to 'sqrt' 5.c:(.text+0x64e): undefined reference to 'atan2' collect2: Id は ステータス1で終了しました。” とメッセージが出るのですが、このメッセージが出たときはプログラムにミスがあるということなのでしょうか。教えてくださいお願いします。 http://plaza.rakuten.co.jp/dragonkkk