I used search.h
for my c program, where I need to put #define _GNU_SOURCE
to the first line in order to introduce multiple hash tables. But after that, errors like undefined reference to 'log10'
and undefined reference to 'PQntuples'
popped up. I certainly need all the packages there, how should I now compile the program? Any help would be deeply appreciated! Thanks.
The headers:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <string.h>
// library for psql
#include <libpq-fe.h>
#include <unistd.h>
#include <time.h>
#include <search.h>
int main(void){
char host[] = "localhost";
char port[] = "5432";
char db_name[] = "db_name";
char user[] = "test_usr";
char password[] = "123456";
sprintf(db_str, "host=%s port=%s dbname=%s user=%s password=%s",
host, port, db_name, user, password);
PGconn *db_connection = DBConnect(db_str);
struct hsearch_data htab;
hcreate_r(10, &htb);
ENTRY e, *ep;
e.key = "test";
e.data = (void *) 1;
hsearch_r(e, ENTER, &ep, &htab);
}
And this was how I compile the file:
gcc -Wall -Wextra -I/home/userX/postgresql/include -L/home/userX/postgresql/lib -lm -lpq -g my_program.c
CodePudding user response:
Specifiy the libraries at the end of the command line
gcc -Wall -Wextra -I/home/userX/postgresql/include \
-L/home/userX/postgresql/lib -g my_program.c -lpq -lm
// ^^^^^^^^
gcc looks at required symbols left to right on the command line.
With the libraries before the source files, when gcc processes the -llib
argument it does not have any requirements and therefore does not "extract" any function from the library.