[英]Compiling a JNI file with c++ postgresql in command prompt getting fatal error
Command executed:执行的命令:
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 file dir - C:\Program Files\libpqxx\include
pqxx 文件目录 -
C:\Program Files\libpqxx\include
I have included the path with -I
.我已将路径包含在
-I
中。
I am using C++ for the backend connection for my Java program using JNI and unable to compile the cpp file, getting error as:我正在使用 C++ 为我的 Java 程序使用 JNI 进行后端连接,但无法编译 cpp 文件,出现以下错误:
HelloJNI.cpp:4:10: fatal error: pqxx/pqxx: No such file or director
4 | #include <pqxx/pqxx>
| ^~~~~~~~~~~
compilation terminated.
The executed code is:执行的代码是:
#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");
}
Since you are using -I"C:\Program Files\libpqxx\include\pqxx" The #include should not be由于您使用的是 -I"C:\Program Files\libpqxx\include\pqxx" #include 不应该是
<pqxx/pqxx>
You can try -I"C:\Program Files\libpqxx\include" to make the search path consistent.您可以尝试 -I"C:\Program Files\libpqxx\include" 使搜索路径保持一致。
Compile the file in visual studio.在 Visual Studio 中编译文件。
Add the paths to the respective fields like additional include directories, linker files in the property.在属性中添加相应字段的路径,例如附加包含目录、链接器文件。
conclude the JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj)
结束
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj)
inside a int main()
method to compile since visual studio cannot compile the files without main method.在
int main()
方法中进行编译,因为 Visual Studio 无法在没有 main 方法的情况下编译文件。
you can follow same as cmdline if your program doesn't have postgresql.如果您的程序没有 postgresql,您可以遵循与 cmdline 相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.