[英]Compiling a JNI file with c++ postgresql in command prompt getting fatal error
执行的命令:
g++ -I"C:\Program Files\Java\jdk-16.0.2\include" -I"C:\Program Files\Java\jdk-16.0.2\include\win32" -I"C:\Program Files\libpqxx\include\pqxx" -shared -o hello.dll HelloJNI.cpp
pqxx 文件目录 - C:\Program Files\libpqxx\include
我已将路径包含在-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 Files\libpqxx\include\pqxx" #include 不应该是
<pqxx/pqxx>
您可以尝试 -I"C:\Program Files\libpqxx\include" 使搜索路径保持一致。
在 Visual Studio 中编译文件。
在属性中添加相应字段的路径,例如附加包含目录、链接器文件。
结束JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj)
在int main()
方法中进行编译,因为 Visual Studio 无法在没有 main 方法的情况下编译文件。
如果您的程序没有 postgresql,您可以遵循与 cmdline 相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.