MySQL C++ Connector: undefined reference to `get_driver_instance' - c++

I've been trying to get the MySQL connector working I've installed both the connector and the mysql client library but I am still getting this error:
obj/Database.obj: In function `Database::connect()':
/home/xeross/alpine/src/server/Database.cpp:13: undefined reference to `get_driver_instance'
collect2: ld returned 1 exit status
make[2]: *** [alpine-server] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
Using Ubuntu 10.04
And my makefile is as follows:
INCLUDES = -I./src -I./src/shared
OUTDIR = bin
INTDIR = obj
OPTIONS = -ggdb -g3 -Wall -O0
alpine-server : Shared.a AsyncServerSocket.obj PlayerHandler.obj PacketHandler.obj Session.obj User.obj Database.obj init
g++ $(INCLUDES) $(OPTIONS) -static \
-pthread \
-lmysqlcppconn-static \
-o $(OUTDIR)/alpine-server src/server/main.cpp \
$(INTDIR)/AsyncServerSocket.obj \
$(INTDIR)/PacketHandler.obj \
$(INTDIR)/Database.obj \
$(INTDIR)/PlayerHandler.obj \
$(INTDIR)/Session.obj \
$(INTDIR)/User.obj \
$(INTDIR)/Shared.a \
-lboost_system \
-lmysqlclient
AsyncServerSocket.obj : src/server/AsyncServerSocket.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/AsyncServerSocket.obj src/server/AsyncServerSocket.cpp
PlayerHandler.obj : src/server/PlayerHandler.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/PlayerHandler.obj src/server/PlayerHandler.cpp
PacketHandler.obj : src/server/PacketHandler.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/PacketHandler.obj src/server/PacketHandler.cpp
Session.obj : src/server/Session.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Session.obj src/server/Session.cpp
User.obj : src/server/User.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/User.obj src/server/User.cpp
Database.obj : src/server/Database.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Database.obj src/server/Database.cpp
# Shared.a
Shared.a : Packet.obj Flags.obj AsyncSocket.obj Log.obj init
ar -cvq $(INTDIR)/Shared.a \
$(INTDIR)/Packet.obj \
$(INTDIR)/Flags.obj \
$(INTDIR)/AsyncSocket.obj \
$(INTDIR)/Log.obj
ranlib $(INTDIR)/Shared.a
Packet.obj : src/shared/packet.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Packet.obj src/shared/packet.cpp
Flags.obj : src/shared/Flags.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Flags.obj src/shared/Flags.cpp
AsyncSocket.obj : src/shared/AsyncSocket.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/AsyncSocket.obj src/shared/AsyncSocket.cpp
Log.obj : src/shared/Log.cpp init
g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Log.obj src/shared/Log.cpp
init:
mkdir -p bin obj
clean:
rm -f $(INTDIR)/*.obj $(INTDIR)/*.a
The Code
// Excerpt from .hpp file
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
// End excerpt
void Database::connect()
{
std::stringstream connString;
connString << "tcp://";
connString << m_host;
connString << ":";
connString << m_port;
m_driver = get_driver_instance(); // This guy is being a *****
m_conn = m_driver->connect(connString.str(), m_user, m_password);
m_conn->setSchema(m_database);
}
What can I do to fix this ?

Finally I could successfully compile a program with C++ connector in Ubuntu 10.10.
Initially I faced the same problem with "undefined reference to `get_driver_instance' " to solve this I declare my driver instance variable of MySQL_Driver type. For ready reference this type is defined in mysql_driver.h file. Here is the code snippet I used in my program.
sql::mysql::MySQL_Driver *driver;
try {
driver = sql::mysql::get_driver_instance();
}
and I compiled the program with -l mysqlcppconn linker option

Thank you so much, I got it fixed too. I had the exact experience.
I am using Eclipse CDT on 64 Bit CentOS and for anyone reading this here are the following steps .
first, be sure that the following are in the code.
include "mysql_driver.h"
include "mysql_connection.h"
using namespace sql::mysql;
Be sure that in Eclipse you have specified in your Eclipse project settings.
the mysql/include and mysql/include/cppconn directories in your include and then also the mysql/lib in the library directory, then and more importantly you specify -lmysqlcppconn.
Be sure you got the -m64 set in the Eclipse compiler options too.
When you run your program, it may complain of missing libmysqlcppconn.so.1 . Do this, copy libmysqlcppconn.so.1.0.5 in your /usr/lib64 directory. Make a link of libmysqlcppconn.so.1 towards libmysqlcppconn.so.1.0.5 within that directory.
Your program should now run.

You need to link with
-lmysqlcppconn -lmysqlcppconn-static
The first library contains the code for the headers in /usr/include/cppconn/, and the second library contains the code found in the headers mysql_driver.h and mysql_connection.h.

You'll need to add -lmysqlcppconn-static after the object files that uses stuff inside that library.

The code would be more helpful than the make file but try adding using namespace sql; to the top of Database.cpp.
// Excerpt from .hpp file
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace sql; // <---- add here

To me was needed -lmysqlclient
g++ -o mariaS2json test.cpp -L/usr/lib/ -lmysqlcppconn -lmysqlclient
And now all works

Related

c++ "fatal error C1083: Cannot open include file: 'curl/curl.h': No such file or directory"

I want to use the https://github.com/tensaix2j/binacpp (a Binance c++ library) and I'm getting the same error again and again. I changed the MakeFile in the library to the correct paths but yeah... Here is my MakeFile:
libcurl_dir=../lib/libcurl-7.56.0
libcurl_include=${libcurl_dir}/include
libcurl_lib=${libcurl_dir}/lib
jsoncpp_dir=../lib/jsoncpp-1.8.3
jsoncpp_include=${jsoncpp_dir}/include
jsoncpp_src=${jsoncpp_dir}/src
libwebsockets_dir=../lib/libwebsockets-2.4.0
libwebsockets_include=${libwebsockets_dir}/include
libwebsockets_lib=${libwebsockets_dir}/lib
libbinacpp_dir=../lib/libbinacpp
libbinacpp_include=${libbinacpp_dir}/include
libbinacpp_lib=${libbinacpp_dir}/lib
build_dir=../lib/libbinacpp/lib
objects=$(build_dir)/jsoncpp.o $(build_dir)/binacpp_utils.o
$(build_dir)/binacpp_logger.o $(build_dir)/binacpp.o $(build_dir)/binacpp_websocket.o
build_include=../lib/libbinacpp/include
$(build_dir)/libbinacpp.so: $(objects)
g++ -I$(libcurl_include) -I$(jsoncpp_include) I$(libwebsockets_include) \
-L$(libcurl_lib) \
-L$(libwebsockets_lib) \
$(objects) \
-shared \
-lcurl -lcrypto -lwebsockets -fPIC -o $#
# Make a new copy of the header too
cp *.h $(build_include)
$(build_dir)/binacpp.o: binacpp.cpp binacpp.h
g++ -I$(libcurl_include) -I$(jsoncpp_include) -c binacpp.cpp -fPIC -o $(build_dir)/binacpp.o
$(build_dir)/binacpp_websocket.o: binacpp_websocket.cpp binacpp_websocket.h
g++ -I$(libwebsockets_include) -I$(jsoncpp_include) -c binacpp_websocket.cpp -fPIC -o $(build_dir)/binacpp_websocket.o
$(build_dir)/binacpp_logger.o: binacpp_logger.cpp binacpp_logger.h
g++ -c binacpp_logger.cpp -fPIC -o $(build_dir)/binacpp_logger.o
$(build_dir)/binacpp_utils.o: binacpp_utils.cpp binacpp_utils.h
g++ -c binacpp_utils.cpp -fPIC -o $(build_dir)/binacpp_utils.o
$(build_dir)/jsoncpp.o: $(jsoncpp_src)/jsoncpp.cpp
g++ -I$(jsoncpp_include) -c $(jsoncpp_src)/jsoncpp.cpp -fPIC -o $(build_dir)/jsoncpp.o
clean:
rm $(build_dir)/*.o
rm $(build_dir)/*.so
If I'm inserting the paths like:
#include "../../libcurl-7.56.0/include/curl/curl.h"
instead of:
#include <curl/curl.h>
then it works but I cant change it for all includes.
Here's the Tree:
binacpp
-example
-lib
--jsoncpp-1.8.3
--libbinacpp
---include
----binacpp.h (File with include issue)
----binacpp_logger.h
----binacpp_websocket.h
----binacpp_utils.h
---lib
--libcurl-7.56.0
--(here's some other not important stuff)
-src
--MakeFile
--(here's some other not important stuff)

How to get a C++ program that uses XWindows to build on Bash on Ubuntu on Windows?

I'm trying to get a C++ program that uses XWindows to build on Bash on Ubuntu on Windows. I understand that Bash on Ubuntu on Windows does not officially support graphical Linux applications, but I would like the program to build regardless if possible. I have tried to reduce my issue as much as possible, hopefully I have provided enough information.
First, I installed the libx11-dev package and the packages associated with it using the command:
sudo apt-get install libx11-dev
Second, my Makefile is the following:
CXX = g++
CXXFLAGS = -Wall -MMD -lX11
EXEC = exe
OBJECTS = main.o window.o
DEPENDS = ${OBJECTS:.o=.d}
${EXEC}: ${OBJECTS}
${CXX} ${CXXFLAGS} ${OBJECTS} -o ${EXEC}
-include ${DEPENDS}
.PHONY: clean
clean:
rm ${EXEC} ${OBJECTS} ${DEPENDS}
Third, the window.h file is the following:
#ifndef __WINDOW_H__
#define __WINDOW_H__
#include <X11/Xlib.h>
class Xwindow {
Display *d;
public:
Xwindow();
};
#endif
Fourth the window.cc file is the following:
#include "window.h"
Xwindow::Xwindow() {
d = XOpenDisplay(NULL);
}
Finally the main.cc file is the following:
#include "window.h"
int main() {
Xwindow xw();
return 0;
}
When I run my Makefile I get the following output:
g++ -Wall -MMD -lX11 -c -o main.o main.cc
g++ -Wall -MMD -lX11 -c -o window.o window.cc
g++ -Wall -MMD -lX11 main.o window.o -o exe
window.o: In function `Xwindow::Xwindow()':
window.cc:(.text+0x12): undefined reference to `XOpenDisplay'
collect2: error: ld returned 1 exit status
Makefile:8: recipe for target 'exe' failed
make: *** [exe] Error 1
Any help is appreciated.
In some environments, including Ubuntu, you need to pass -l arguments after the object files that use these libraries.
Also, -l don't belong in CXXFLAGS, they should be in LIBS which goes after ${OBJECTS}.

Makefile and errors compiling separate files from a big project

I'm trying to compile a small .cpp file but running into issues since this file utilizes functions found in many other files. Basically, I cloned the ZCash repo and am currently trying to use specific files in that project to do some testing. When I compile the entire ZCash repo that I cloned everything works perfectly, but when I try to compile the files I'm working with some of them get syntax errors.
My Makefile:
CC = g++
CFLAGS = -std=c++11 -Wall -g -c
IDIR = /home/parallels/zcash/src/
INC = -I /home/parallels/zcash/src/ -I
/home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
VPATH = /home/parallels/zcash/src/
all: Main.o KeyGen.o prf.o uint256.o sha256.o
$(CC) Main.o KeyGen.o prf.o uint256.o sha256.o -o keygenerator
Main.o: Main.cpp
$(CC) $(CFLAGS) Main.cpp $(INC)
KeyGen.o: KeyGen.cpp
$(CC) $(CFLAGS) KeyGen.cpp $(INC)
prf.o: prf.cpp
$(CC) $(CFLAGS) prf.cpp $(INC)
uint256.o: uint256.cpp
$(CC) $(CFLAGS) uint256.cpp $(INC)
sha256.o: sha256.cpp
$(CC) $(CFLAGS) sha256.cpp $(INC)
clean:
rm -rf *.o keygenerator
The .cpp files being compiled reference these other files:
Main.cpp:
#include "KeyGen.h"
#include "uint252.h"
#include "uint256.h"
#include <string>
KeyGen.cpp
#include "KeyGen.h"
#include "prf.h"
prf.cpp
#include "prf.h"
#include "crypto/sha256.h"
uint256.cpp
#include "uint256.h"
#include "utilstrencodings.h"
#include <stdio.h>
#include <string.h>
sha256.cpp
#include "crypto/sha256.h"
#include "crypto/common.h"
#include <string.h>
#include <stdexcept>
So, after I try to compile these files I get the following error:
g++ -std=c++11 -Wall -g -c Main.cpp -I /home/parallels/zcash/src/ -I /home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
g++ -std=c++11 -Wall -g -c KeyGen.cpp -I /home/parallels/zcash/src/ -I /home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
g++ -std=c++11 -Wall -g -c prf.cpp -I /home/parallels/zcash/src/ -I /home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
g++ -std=c++11 -Wall -g -c uint256.cpp -I /home/parallels/zcash/src/ -I /home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
g++ -std=c++11 -Wall -g -c sha256.cpp -I /home/parallels/zcash/src/ -I /home/parallels/zcash/depends/x86_64-unknown-linux-gnu/include/
In file included from /usr/include/x86_64-linux-gnu/bits/byteswap.h:35:0,
from /usr/include/endian.h:60,
from /usr/include/x86_64-linux-gnu/bits/waitstatus.h:64,
from /usr/include/stdlib.h:42,
from /home/parallels/zcash/src/crypto/sha256.h:9,
from sha256.cpp:5:
/home/parallels/zcash/src/compat/endian.h:111:17: error: expected unqualified-id before ‘__extension__’
inline uint16_t htobe16(uint16_t host_16bits)
^
In the offending file /home/parallels/zcash/src/compat/endian.h:111:17, htobe16 is used as follows:
#if HAVE_DECL_HTOBE16 == 0
inline uint16_t htobe16(uint16_t host_16bits) //line #111
{
return bswap_16(host_16bits);
}
#endif // HAVE_DECL_HTOBE16
There also exists a file: /usr/include/endian.h who references htobe16 as follows.
# include <bits/byteswap.h> //line #60
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define htobe16(x) __bswap_16 (x)
# define htole16(x) (x)
# define be16toh(x) __bswap_16 (x)
# define le16toh(x) (x)
Note:
All the .cpp file I am compiling are located in both the ZCash repo that I cloned and the new folder I created (wondering if this might be the issue).
The folder I'm currently working on contains :
KeyGen.cpp
KeyGen.h
Main.cpp
Makefile
prf.cpp
prf.h
serialize.h
sha256.cpp
sha256.h
uint252.h
uint256.cpp
uint256.h
Sorry for the long post, I truly appreciate your time and would really appreciate some help!
The inline functions are already declared by several macros with the same name. Similar to this question: error: expected unqualified-id before ‘__extension__’ in Linux (Cent OS)
Could have to do with a unconfigured part of the slice you're building from the whole project as this Github discussion seems to indicate:
https://github.com/bitcoin/bitcoin/issues/5920

Using SOCI (sql wrapper) on Xubuntu, simple program fails while compilig

I want to finally get to know how to write an application which uses database. I chose C++, PostgreSQL and SOCI (SQL wrapper to C++). I use Xubuntu 11.4 and installed everyting which was necessary to run a simple program.
To use SOCI I installed:
1) libboost-dev
2) libpq-dev
3) libtool
4) SOCI, using this: http://soci.sourceforge.net/doc/backends/postgresql.html#required and I compiled SOCI with this command: cmake cmake -G "Unix Makefiles" -DWITH_BOOST=ON -DWITH_POSTGRESQL=ON ../
My simple program is veeery simple:
#include "soci-postgresql.h"
int main(int argc, char **argv){
soci::session sql(postgresql, "testDB");
return 0;
}
I compile it like this:
g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq
but it gives me error:
test.cpp:1:29: fatal error: soci-postgresql.h: No such file or
directory compilation terminated.
How to fix this, whats wrong? Did I miss to install something?
Some more infos:
/usr/local/include/soci$ ls
backend-loader.h postgresql soci-platform.h
blob-exchange.h prepare-temp-type.h soci-simple.h
blob.h procedure.h statement.h
boost-fusion.h ref-counted-prepare-info.h transaction.h
boost-gregorian-date.h ref-counted-statement.h type-conversion.h
boost-optional.h row-exchange.h type-conversion-traits.h
boost-tuple.h row.h type-holder.h
connection-pool.h rowid-exchange.h type-ptr.h
empty rowid.h unsigned-types.h
error.h rowset.h use.h
exchange-traits.h session.h use-type.h
into.h soci-backend.h values-exchange.h
into-type.h soci-config.h values.h
once-temp-type.h soci.h version.h
/usr/local/include/soci/postgresql$ ls
common.h soci-postgresql.h
/usr/local/lib$ ls
libCOS4.a libomniORB4.so.1
libCOS4.so libomniORB4.so.1.6
libCOS4.so.1 libomnithread.a
libCOS4.so.1.6 libomnithread.so
libCOSDynamic4.a libomnithread.so.3
libCOSDynamic4.so libomnithread.so.3.4
libCOSDynamic4.so.1 libsoci_core.a
libCOSDynamic4.so.1.6 libsoci_core.so
libomniCodeSets4.a libsoci_core.so.3.1
libomniCodeSets4.so libsoci_core.so.3.1.0
libomniCodeSets4.so.1 libsoci_empty.a
libomniCodeSets4.so.1.6 libsoci_empty.so
libomniConnectionMgmt4.a libsoci_empty.so.3.1
libomniConnectionMgmt4.so libsoci_empty.so.3.1.0
libomniConnectionMgmt4.so.1 libsoci_postgresql.a
libomniConnectionMgmt4.so.1.6 libsoci_postgresql.so
libomniDynamic4.a libsoci_postgresql.so.3.1
libomniDynamic4.so libsoci_postgresql.so.3.1.0
libomniDynamic4.so.1 pkgconfig
libomniDynamic4.so.1.6 python2.7
libomniORB4.a python3.2
libomniORB4.so
I also tried this one: g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I /usr/local/include/soci/postgresql and got the error:
g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I
/usr/local/include/soci/postgresql In file included from test.cpp:1:0:
/usr/local/include/soci/postgresql/soci-postgresql.h:27:26: fatal
error: soci-backend.h: No such file or directory compilation
terminated.
g++ test.cpp -Iyour_soci_dir -lsoci_core -lsoci_postgresql -ldl -lpq
your_soci_dir is directory with installed soci include files.
You need to set the search path with -I option.
g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I<path to headers>
^^^^^^^^^^^^^^^^^ Put the path
You need to provide all paths. I guess in your case, it should be like this :
g++ test.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I/usr/local/include/soci/postgresql -I/usr/local/include/soci
There is also another option (which is more used in makefiles) : to use pkg-config. Here you can find such example :
PROGRAM = test
PROGRAM_FILES = test.c
CFLAGS += -g $(shell pkg-config --cflags xmlsec1-nss)
LDFLAGS += -g
LIBS += $(shell pkg-config --libs xmlsec1-nss)
all: $(PROGRAM)
%: %.c
$(cc) $(PROGRAM_FILES) $(CFLAGS) $(LDFLAGS) -o $(PROGRAM) $(LIBS)
clean:
#rm -rf $(PROGRAM)
In your case, it would be something like this (not tested) :
PROGRAM = test
PROGRAM_FILES = test.cpp
CC=g++
CXXFLAGS += -g $(shell pkg-config --cflags soci_core) $(shell pkg-config --cflags soci_postgresql)
LDFLAGS += -g
LIBS += $(shell pkg-config --libs soci_core) $(shell pkg-config --libs soci_postgresql)
all: $(PROGRAM)
%: %.cpp
$(CC) $(PROGRAM_FILES) $(CFLAGS) $(LDFLAGS) -o $(PROGRAM) $(LIBS)
clean:
#rm -rf $(PROGRAM)
Ok guys, I found a solution on a polish programming website.
My new code (socitest.cpp):
#include <iostream>
#include <soci.h>
#include <postgresql/soci-postgresql.h>
int main(int argc, char **argv)
{
try
{
soci::session sql(soci::postgresql, "dbname=testDB");
}
catch (soci::postgresql_soci_error const & e)
{
std::cerr << "PostgreSQL error: " << e.sqlstate() << " " << e.what() << std::endl;
}
catch (std::exception const & e)
{
std::cerr << "Some other error: " << e.what() << std::endl;
}
return 0;
}
And I have to compile it :
g++ socitest.cpp -lsoci_core -lsoci_postgresql -ldl -lpq -I
/usr/local/include/soci -I /usr/include/postgresql -o socitest
It should compile but it might not run. If it's not running, I have to do sth more:
1) create soci.conf file:
touch /etc/ld.so.conf.d/soci.conf
2) edit my soci.conf with gedit:
gedit /etc/ld.so.conf.d/soci.conf
3) paste in soci.conf:
/usr/local/lib64
(for Xubuntu x64) or
/usr/local/lib
(for Xubuntu x32) and save file
4) run command:
ldconfig
5) run my compiled socitest like this: ./socitest and it's wooooorking !!! :D
Thanks all of you for helping me:)
Above example code can be compiled with just one command, without soci.conf and ldconfig:
g++ socitest.cpp -o socitest -I/usr/include/postgresql -I/usr/local/include/soci -L/usr/local/lib64/ \
-lsoci_core -lsoci_postgresql -Wl,-rpath=/usr/local/lib64/
For 32 bit unix use /usr/local/lib instead of /usr/local/lib64

Linkage error using shared dll with minimal C++/SWIG/Lua code

This is a really specific compilation problem involving C++, SWIG and Lua.
I have a really simple base code :
[AClass.hpp]
class AClass {
public:
AClass();
};
[AClass.cpp]
#include "AClass.hpp"
AClass::AClass() {}
[main.cpp]
#include "AClass.hpp"
int main() {
AClass my_a;
}
At this point, there is no matter with compilation.
I first compile the class in libengine.dll and then use the shared library to build the executable.
Let's introduce a SWIG module, and add it to the dll :
[AClass.i]
%module M_AClass
%{
#include "AClass.hpp"
%}
%include "AClass.hpp"
Henceforth, when linking everything in an executable, I got the following error :
g++ -c main.cpp
g++ -c AClass.cpp
swig.exe -c++ -lua AClass.i
g++ -Iinclude -c AClass_wrap.cxx
g++ AClass.o AClass_wrap.o -shared -o libengine.dll -Wl,--out-implib,libengine.dll.a -L. -llua5.1
Creating library file: libengine.dll.a
g++ main.o libengine.dll.a -o main.exe
main.o:main.cpp:(.text+0x16): undefined reference to `AClass::AClass()'
collect2: ld returned 1 exit status
Would anyone have a clue ? I tried looking into the dll with nm but I can't figure how adding another .o to the shared library can "hide" a method (this isn't specific to constructors).
To reproduce the context, here are the necessary files to put in a directory to build the test :
include/ # Contains "lauxlib.h", "lua.h" & "luaconf.h"
liblua5.1.dll
AClass.hpp
AClass.cpp
AClass.i
main.cpp
Makefile
And finally, here is the Makefile content :
ifneq (,$(findstring Linux,$(shell uname -o)))
EXEC := main
LIB := libengine.so
LIB_FLAGS := -o $(LIB)
else
EXEC := main.exe
LIB := libengine.dll.a
LIB_FLAGS := -o libengine.dll -Wl,--out-implib,$(LIB)
#NO DIFFERENCE using ".dll.a" as in CMake (option: -Wl,--out-implib,) or only ".dll"
ifdef SystemRoot
# Pure Windows, no Cygwin
RM := del /Q
endif
endif
LANG_LIB := -L. -llua5.1
LANG_INC := include
LANG_SWIG := -lua
all: clean $(EXEC)
clean:
$(RM) main *.exe *_wrap.cxx *.o libengine.*
$(EXEC): main.o $(LIB)
g++ $^ -o $#
main.o: main.cpp
g++ -c $<
#NO PB without dependency to AClass_wrap.o
$(LIB): AClass.o AClass_wrap.o
g++ $^ -shared $(LANG_LIB) $(LIB_FLAGS)
AClass.o: AClass.cpp
g++ -fPIC -c $<
AClass_wrap.o: AClass_wrap.cxx
g++ -fPIC -I$(LANG_INC) -c $<
AClass_wrap.cxx: AClass.i
swig -c++ $(LANG_SWIG) $<
This was tested under Windows Seven, with MingGW g++ v4.5.2, SWIG 2.0.2 and Lua5.1.
EDIT: The problem also appear when SWIG-exporting to tcl. However, there is absolutely no problem compiling under Linux. I compared the generated AClass_wrap.cxx, they are similar.
g++ under mingw might require __declspec(dllimport/export)