4

I have cuda code which makes a call to a function present in a .c file whose header file I have included in my cuda code. So, in all I have a header file, a C file for that header file, and a CUDA code. When I am compiling my CUDA code using nvcc and specifying my cuda code name and c file name, then I am getting undefined reference to the functions I called in my CUDA code which are actually present in my C file. Please help me understand what am I doing wrong and how can I fix my mistake.

Ok I am pasting my code below... I did not post it initially because I thought its a linker error or something.

 #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;
    }
}

The dbConnection.c file has :

#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;
}

So when I am doing:

nvcc DB.cu dbConnection.c -o DB 

I am getting undefined reference to make connection. Also, I will be transferring the data I get from DB to GPGPU later and that is the whole point of this exercise so please do not say I have no CUDA calls here. This is a code still under development.

talonmies
  • 70,661
  • 34
  • 192
  • 269
user1439690
  • 659
  • 1
  • 11
  • 26

1 Answers1

5

Your external function is in a .c file, so the host compiler compiles it using the C language naming/calling convention. nvcc, on the other hand, is a C++ compiler by default, so it defaults to C++ naming/calling convention. You need to tell the C++ compiler to look for an external "C" function for makeConnection, by declaring it as such in your header and/or forward declaration in the .cu file.

extern "C"
int makeConnection(PGconn** conn,const char* connInfo);
harrism
  • 26,505
  • 2
  • 57
  • 88