4

I'm trying to compile a C++ program that utilizes sqlite3. I'm using this makefile:

CXX = g++ 
CC = gcc 
CFLAGS = -c -O2 
CXXFLAGS = -Wall -O3 -std=c++11
SQLFLAGS = -DSQLITE_THREADSAFE=0

OUTPUTBIN = bot
OUTPUTDIR = ./bin/
MKDIR = mkdir -p $(OUTPUTDIR) 
OBJECTC = sqlite3.o
CSOURCES = sqlite3.c  
CXXSOURCES = main.cpp bot.cpp


all: project

project: createdir sql compilecpp


createdir:
    $(MKDIR)

sql:
    $(CC) $(CSOURCES) $(SQLFLAGS) $(CFLAGS) -o $(OUTPUTDIR)$(OBJECTC)

compilecpp:
    $(CXX) $(OUTPUTDIR)$(OBJECTC) $(CXXSOURCES) $(CXXFLAGS) -o $(OUTPUTDIR)$(OUTPUTBIN)

But outputs these errors:

akf@akf-v5 ~/Documents/Proletarian/c++ $ make
mkdir -p ./bin/ 
gcc  sqlite3.c   -DSQLITE_THREADSAFE=0 -c -O2  -o ./bin/sqlite3.o
g++  ./bin/sqlite3.o main.cpp bot.cpp -Wall -O3 -std=c++11 -o ./bin/bot
./bin/sqlite3.o: In function `unixDlError':
sqlite3.c:(.text+0x170f4): undefined reference to `dlerror'
./bin/sqlite3.o: In function `unixDlClose':
sqlite3.c:(.text+0x5de9): undefined reference to `dlclose'
./bin/sqlite3.o: In function `unixDlSym':
sqlite3.c:(.text+0x5e01): undefined reference to `dlsym'
./bin/sqlite3.o: In function `unixDlOpen':
sqlite3.c:(.text+0x5e21): undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
make: *** [compilecpp] Error 1

I'm extremely confused as to what's causing this. I can see that sqlite3 is a C program, but I didn't think it would cause any issues.

ES-AKF
  • 103
  • 1
  • 2
  • 5
  • 1
    Add `-ldl` to your linker command line. – Frédéric Hamidi Jan 28 '15 at 12:59
  • 1
    Googling `dlerror undefined symbol` returns http://stackoverflow.com/questions/956640/linux-c-error-undefined-reference-to-dlopen as the first hit. – CiaPan Jan 28 '15 at 13:36
  • Thank you both, and @CiaPan I found this too, but I was unsure how to add it to my makefile or if it was at all relevant to my issue. I'm new to C++, which is probably more the issue than anything else. – ES-AKF Jan 28 '15 at 23:20

2 Answers2

7

The error messages tell that dlerror, dlclose, dlsym and dlopenare used but can't be found. Those functions are part of the dynamic link loader. You have to link the dynamic linker, too. Add -ldl to your link flags. See also dlopen manpage for your system.

johannes
  • 15,807
  • 3
  • 44
  • 57
  • 1
    That seems perfect, I've just read up on it, thank you. The only thing is, I'm not sure what to place in the makefile? And is it important where I place it? I'm new to makefiles, sorry to be a pain – ES-AKF Jan 28 '15 at 13:19
  • The easy thing is it to add it to your CXX call in the compilecpp target, the Makefile has lots of room for improvement (I'd let that to others who know more about best practices there, though) – johannes Jan 28 '15 at 13:26
  • Perfect, thank you. I was putting it with sql by mistake. Looks like I need some sleep more than anything! – ES-AKF Jan 28 '15 at 13:35
1

A bit late, but - the simplest possible Makefile:

all: sqlite3

sqlite3: sqlite3.o shell.o
    gcc sqlite3.o shell.o -lpthread -ldl -o sqlite3

sqlite3.o: sqlite3.c sqlite3.h
    gcc -c sqlite3.c -lpthread -ldl -o sqlite3.o

shell.o: shell.c
    gcc -c shell.c -lpthread -o shell.o

clean:
    rm *.o
    rm sqlite3
asdfgh
  • 2,823
  • 4
  • 21
  • 26