2012-07-14 65 views
2

我正在關注here中的示例,我的代碼是相同的。在Linux中使用MYSQL.h編譯C++程序

當我將mysql_config --libs和mysql_config --cflags鍵入控制檯時,他解釋說,我得到與他顯示的相同的輸出。然而,當我嘗試使用編譯

g++ -o output-file $(mysql_config --cflags) test.cpp $(mysql_config --libs)

我得到的錯誤:

test.cpp:3:25: error: mysql.h: No such file or directory
test.cpp: In function âint main()â:
test.cpp:6: error: âMYSQLâ was not declared in this scope
test.cpp:6: error: âconnâ was not declared in this scope
test.cpp:7: error: âMYSQL_RESâ was not declared in this scope
test.cpp:7: error: âresâ was not declared in this scope
test.cpp:8: error: âMYSQL_ROWâ was not declared in this scope
test.cpp:8: error: expected `;' before ârowâ
test.cpp:13: error: âmysql_initâ was not declared in this scope
test.cpp:17: error: âmysql_real_connectâ was not declared in this scope
test.cpp:18: error: âmysql_errorâ was not declared in this scope
test.cpp:19: error: âexitâ was not declared in this scope
test.cpp:22: error: âmysql_queryâ was not declared in this scope
test.cpp:23: error: âmysql_errorâ was not declared in this scope
test.cpp:24: error: âexitâ was not declared in this scope
test.cpp:27: error: âmysql_use_resultâ was not declared in this scope
test.cpp:31: error: ârowâ was not declared in this scope
test.cpp:31: error: âmysql_fetch_rowâ was not declared in this scope
test.cpp:35: error: âmysql_free_resultâ was not declared in this scope
test.cpp:36: error: âmysql_closeâ was not declared in this scope

當我嘗試 'whereis mysql的' 它顯示在/ usr/bin中/ MySQL的,/ usr/lib中/ MySQL和/ usr/share/mysql,但我不確定mysql.h的確切位置。我正在工作的服務器的管理員說他安裝了MySQL,我確實可以使用phpMyAdmin創建/操作表。

另外,請給我關於這個特定問題的建議。我知道MySQL的C++包裝,但我現在只想使用C API。謝謝!

回答

2

這裏的代碼編譯罰款(我只需要包括退出())stdlib.h)。 MySQL頭文件應該位於/ usr/include/mysql中,並且由於「mysql_config --cflags」的輸出相同,所以您可能會丟失/破壞程序包。

嘗試重新安裝他在文章開頭列出的軟件包,或者檢查頭文件在發行版中的哪個軟件包。我正在使用Ubuntu,這裏的頭文件包是libmysqlclient15-dev。

+0

是的,我相信你是對的。經過一段時間的捕魚後,似乎我沒有正確的軟件包來開發使用MySQL的應用程序。我有點擔心自己安裝它們,儘管服務器的管理員在那裏安裝了MySQL。雖然,我確實有'管理員'訪問服務器,但我不知道添加它們是否有任何潛在的危害。 – Aaron 2012-07-14 18:38:07

+1

@Aaron您需要在服務器上安裝libmysqlclient,但需要在您正在開發的機器上使用devel頭文件。 – fbafelipe 2012-07-14 18:43:02

+1

哦,我明白了。 devel頭文件只是在本地編譯應用程序,然後我可以將它們上傳到服務器以正確運行? – Aaron 2012-07-14 19:24:09

1

該文件應該位於/ usr/include/mysql下的某個位置。你可以嘗試使用類似find /usr -name mysql.h

+0

嗯,我在/ usr/include中沒有mysql目錄。在這種情況下,我認爲管理員可能沒有安裝所有必要的軟件包。 – Aaron 2012-07-14 18:10:07