簡體   English   中英

從我的CUDA代碼中調用我的C函數

[英]Make my C function call from my CUDA code

我有cuda代碼,它調用.c文件中存在的函數,該文件的頭文件已包含在我的cuda代碼中。 所以,總而言之,我有一個頭文件,一個用於該頭文件的C文件和一個CUDA代碼。 當我使用nvcc編譯我的CUDA代碼並指定我的cuda代碼名稱和c文件名時,我得到了我在CUDA代碼中調用的函數的未定義引用,這些函數實際存在於我的C文件中。 請幫助我理解我做錯了什么,如何解決我的錯誤。

好吧我在下面粘貼我的代碼...我最初沒有發布它因為我認為它是鏈接器錯誤或其他什么。

 #include "dbConnection.h"
 #include "error.h"
 #include "libpq-fe.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include "appCompileSwitches.h"


int makeConnection(PGconn** conn,const char* connInfo);
void executeQuery(PGconn* conn,PGresult** res,char* statement,int* rows,int* columns);


/***************************************
* main(), enough said
****************************************/
int main(int argc, char **argv)
{

    PGconn *conn = NULL;
    PGresult *res= NULL;
    float** result;
    char* statement = "select visit_no,brand_name from visit_sample limit 3";
    int rows=0,columns=0; // WILL BE USED TO CUDAMALLOC gpu memory
    const char* connInfo = "dbname = moxy";

    if(!makeConnection(&conn,connInfo))
    {
            printf("failed to connect to Database!\n");
            return FAILURE;
    }
}

dbConnection.c文件具有:

#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"
#include <string.h>
#include "dbConnection.h"
#include "error.h"
#include "appCompileSwitches.h"

/****************************************************
* close database connection, given connecton info
****************************************************/
static void closeConnection(PGconn *conn)
{

    /* close the connection to the database and cleanup */
    PQfinish(conn);
}

/****************************************************
* connect to the database
* given the connInfo
****************************************************/
extern int makeConnection(PGconn** conn,const char* connInfo)
{
    /* Make a connection to the database */
    *conn = PQconnectdb(connInfo);
    if (PQstatus(*conn) != CONNECTION_OK)
    {
            fprintf(stderr, "Connection to database failed: %s",PQerrorMessage(*conn));
            PQfinish(*conn);
            return FAILURE;
    }
    return SUCCESS;
}

所以當我這樣做時:

nvcc DB.cu dbConnection.c -o DB 

我得到未定義的引用連接。 此外,我將把我從DB獲得的數據轉移到GPGPU,這是本練習的重點,所以請不要說我這里沒有CUDA調用。 這是一個仍在開發中的代碼。

您的外部函數位於.c文件中,因此主機編譯器使用C語言命名/調用約定對其進行編譯。 另一方面,nvcc默認是C ++編譯器,因此它默認為C ++命名/調用約定。 您需要告訴C ++編譯器為makeConnection尋找外部“C”函數, makeConnection是在.cu文件的頭文件和/或前向聲明中聲明它。

extern "C"
int makeConnection(PGconn** conn,const char* connInfo);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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