在命令提示符中使用c++ postgresql编译JNI文件得到致命错误



执行命令:

g++ -I"C:Program FilesJavajdk-16.0.2include" -I"C:Program FilesJavajdk-16.0.2includewin32" -I"C:Program Fileslibpqxxincludepqxx" -shared -o hello.dll HelloJNI.cpp

pqxx file dir -C:Program Fileslibpqxxinclude

我已经包含了-I的路径。

我使用c++作为我的Java程序使用JNI的后端连接,无法编译cpp文件,得到错误如下:


HelloJNI.cpp:4:10: fatal error: pqxx/pqxx: No such file or director
4 | #include <pqxx/pqxx>
|          ^~~~~~~~~~~
compilation terminated.

执行的代码是:

#include <jni.h>       // JNI header provided by JDK
#include <iostream>    // C++ standard IO header
#include "HelloJNI.h"  // Generated
#include <pqxx/pqxx>
#include <string>
using namespace std;
// Implementation of the native method sayHello()
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) 
{
try
{
std::string connectionString = "host=localhost port=5432 dbname=postgres user=postgres password =caNarain@2002";
pqxx::connection connectionObject(connectionString.c_str());
pqxx::work worker(connectionObject);
pqxx::result response = worker.exec("SELECT * FROM books ORDER BY BOOK_ID");
for (size_t i = 0; i < response.size(); i++)
{
std::cout << response[i][0] << " " << response[i][1] << " " << response[i][2] << " " << response[i][3] << " " << response[i][4] << " " << response[i][5] << std::endl;
}
return;
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
system("pause");
}

由于您使用的是-I"C:Program Fileslibpqxxincludepqxx"#include不应该是

<pqxx/pqxx>

你可以试试-I"C:Program Fileslibpqxxinclude"使搜索路径一致。

在visual studio中编译文件

在相应的字段中添加路径,如附加的包含目录,属性中的链接器文件。

总结JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj)

int main()方法中编译,因为visual studio不能编译没有main方法的文件。

如果你的程序没有postgresql,你可以像cmdline一样使用

相关内容

最新更新