#undefined reference to function

24 messages · Page 1 of 1 (latest)

zinc flame
#

I am trying to use the wal implementation of sqlite. sqlite3WalOpen and sqlite3WalClose are in the wal header file, and its definition exists but when I try to compile the program that uses the wal implementation I get:

/usr/bin/ld: /tmp/ccmbSleS.o: in function `main':
usewal.c:(.text+0x143): undefined reference to `sqlite3WalOpen'
/usr/bin/ld: usewal.c:(.text+0x183): undefined reference to `sqlite3WalClose'
collect2: error: ld returned 1 exit status

This is the code

#include <stdio.h>
#include "wal.h"

int main() {
    printf("got here\n");
    sqlite3 *db;
    int rc;

    // Open the SQLite database.
    rc = sqlite3_open("mydatabase.db", &db);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
        return rc;
    }

    sqlite3_file *pDbFd = NULL;

    // Obtain the database file descriptor using SQLITE_FCNTL_FILE_POINTER.
    rc = sqlite3_file_control(db, "main", SQLITE_FCNTL_FILE_POINTER, &pDbFd);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Error obtaining the database file descriptor: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return rc;
    }

    const char *zWalName = "mydatabase-wal";
    int bNoShm = 0; // Use the default WAL mode, not heap-memory mode
    i64 mxWalSize = 0; // Don't truncate the WAL file

    Wal *pWal = NULL;
    rc = sqlite3WalOpen(sqlite3_vfs_find(zWalName), pDbFd, zWalName, bNoShm, mxWalSize, &pWal);

    if (rc == SQLITE_OK) {
        printf("WAL file opened successfully.\n");

        // Perform WAL-related operations here if needed.

        // Close the WAL file when done.
        sqlite3 *dbToClose = 0; // Pass a NULL pointer for the database connection to close
        sqlite3WalClose(pWal, dbToClose, 1, 0, 0);
    } else {
        fprintf(stderr, "Error opening WAL file: %s\n", sqlite3_errstr(rc));
    }

    Close the SQLite database.
    sqlite3_close(db);

    return 0;
}

program runs successfully without sqlite3WalX

noble kayakBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

atomic dome
#

Which file defines sqlite3WalOpen?

dapper jetty
#

This is a linking error. It means you're not linking against the implementation of these functions correctly (i.e. the library binary is not compatible with the headers in some way, either could be wrong).

Perhaps you're missing a dependency?
Perhaps you didn't link the library correctly?

zinc flame
#

sqlite3.c defines it. I run the program with
gcc -o usewal usewal.c sqlite3.c

dapper jetty
#

does sqlite3.c definitely implement sqlite3WalOpen?

zinc flame
#

Yes

dapper jetty
#

can we see the definition?

#

just the function declaration part and maybe a couple lines around

#

just as a sanity check

zinc flame
#

Yes, I can. I figured I didn't add the header file (wal.h) to the sqlite3.c. I have done that now, and it has led me to more code errors, which shows this particular problem has been solved

dapper jetty
#

including the header doesn't change a linking error

#

Fixing those other errors may fix this error. But also they are probably just masking it

noble kayakBOT
#

dapper jetty
#

linking errors are the last errors to be checked so it can't get to them until you can actually compile successfully

#

if you cause a normal compile error your linking errors miraculously disappear

#

;compile

void foo();//no definition

int main() {
  foo();
}
```will produce a linking error
frail pelicanBOT
#
Compiler Output
/opt/compiler-explorer/gcc-13.2.0/bin/../lib/gcc/x86_64-linux-gnu/13.2.0/../../../../x86_64-linux-gnu/bin/ld: /tmp/ccsZmDAf.o: in function ​`main':
<source>:4: undefined reference to ​`foo'
collect2: error: ld returned 1 exit status
Build failed
zinc flame
dapper jetty
#

;compile

void foo();

int main() {
  int i = foo();
}
```but this doesn't produce a linking error. Your error hasn't gone. If this compile it would still have a linking error. Its just not being shown because you can't get to the stage where its checked
frail pelicanBOT
#
Compiler Output
<source>: In function 'main':
<source>:4:11: error: void value not ignored as it ought to be
    4 |   int i = foo();
      |           ^~~
Build failed
zinc flame
#

I guess the solution would be to keep solving the present compiler errors and if it reappears, I come back to it

dapper jetty
#

yeah its likely the compile errors are related