C言語でアレコレ処理できると嬉しいのでちょっとずつC言語に慣れようと努力している。
libcurl は curl easy interface から使うのが楽
#include <stdio.h> #include <curl/curl.h> int main(void){ CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "http://headlines.yahoo.co.jp/hl"); res = curl_easy_perform(curl); if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res)); curl_easy_cleanup(curl); } return 0; }
インストール
brew install curl #(略)なんたらメッセージ -L/usr/local/opt/curl/lib -I/usr/local/opt/curl/include
エラー onz
ld: warning: path '/usr/local/opt/curl/lib/libcurl.a' following -L not a directory Undefined symbols for architecture x86_64: "_curl_easy_init", referenced from: _main in cctEYxkV.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status
コンパイルエラー。 libcurl のコンパイル済みライブラリが見つからないので、関数の実体が何処にあるかわからない的なエラーメッセージ?
私は、このエラーの理由がわからずMac OSXでのコンパイルを諦めていた。C言語初心者&田舎暮らしの辛さである。誰に聞くことも出来ない孤独だ。。。
突然解決した
gcc curl_test.c -l curl
コンパイル出来た
takuya@air:~/Desktop$ ./a.out | nkf -w | head <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=euc-jp"> <meta http-equiv="Content-Style-Type" content="text/css"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <link rel="canonical" href="http://headlines.yahoo.co.jp/hl"> <title>Yahoo!ニュース</title> <meta name="description" content="新聞・通信社などが配信するニュースを掲載。"> <meta name="keywords" content="ニュース,新聞,雑誌"> curl_easy_perform() failed: Failed writing received data to disk/application takuya@air:~/Desktop$
動いた!動いた。
落ち着いたら出来るものですね。 -l オプションと -L/usr/local/opt/curl/lib の違いが結局わからないままだけど。前に進んだ