-1

I downloaded the file https://github.com/duckdb/duckdb/releases/download/v0.3.2/libduckdb-linux-amd64.zip, but I do not know how to use it in C++.

This file contains the following files:

  • duckdb.h

    duckdb.hpp

    libduckdb.so

What should I do after downloading?

The following code that I run:

#include "duckdb.hpp"

using namespace duckdb;

int main () {
DuckDB db (nullptr);
Connection con (db);

con.Query ("CREATE TABLE integers (i INTEGER)");
con.Query ("INSERT INTO integers VALUES (3)");
auto result = con.Query ("SELECT * FROM integers");
result-> Print ();
}

Gives the following error:

undefined reference to `duckdb :: DuckDB :: DuckDB (char const *, duckdb :: DBConfig *) '
  • Can you show your g++ command line that you used to compile your application? Looks like you missed linking to the library. – drescherjm Apr 07 '22 at 13:13
  • g++ -fdiagnostics-color=always -g /home/dblab/codes/cpp/duck-db/main.cpp -o /home/dblab/codes/cpp/duck-db/main – user18736718 Apr 07 '22 at 13:29
  • That shows you did not link to the library. If you are using VSCode you likely need to edit your tasks.json and add the linker arguments if you are not using code runner. – drescherjm Apr 07 '22 at 13:31
  • An excellent guide about Shared libraries with gcc and g++ on Linux [link](https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html) – user18736718 Apr 07 '22 at 19:04

1 Answers1

1

While compiling your code, need to link ur library i.e. use .so or .a file name.

If library name foo.so, then compilation will be done as follows(.

g++ -lfoo xyz.cpp -o xyz

keep the library in same folder as cpp file, otherwise you will need specify path as well with -L

To specify a directory to search for (binary) libraries, you just use -L:

-L/data[...]/lib

To specify a directory to search for include header files you use -I:

-I/data[...]/include/

HrishDev
  • 11
  • 2
  • I used the following command: 'g ++ -llibduckdb main.cpp -o main' I get the following error: / usr / bin / ld: cannot find -llibduckdb collect2: error: ld returned 1 exit status The name of the library is libduckdb.so and it is in the directory where the main.cpp file is located. – user18736718 Apr 07 '22 at 14:10