簡體   English   中英

在命令提示符下使用 c++ postgresql 編譯 JNI 文件出現致命錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM