1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | ######Makefile.main#############
######This contains all the dependencies, for example###########
executable: executable.o
$(CXX) executable.o -o executable $(LINK)
executable.o: executable.cpp
$(CXX) -c executable.cpp -o executable.o $(INCLUDES)
######This needs to be set only once! It's machine independent!###########
#######Makefile.mylaptop###########
#######This contains the paths to the local compilers, for example#########
CXX = clang++
INCLUDES = -I/usr/local
LINK = -L/usr/local -lfftw3
#######Makefile.cluster###########
#######This contains the paths to the remote compilers, for example#########
CXX = /path/to/c++/compiler/g++
INCLUDES = -I/path/to/fftw_library/include
LINK = -L/path/to/fftw_library/lib -lfftw3
#######Makefile###########
#######This contains only three lines, and that's the file that "make" will read!############
me = ${THIS}
include Makefile.$(me)
include Makefile.main
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ////////////*myfunc.h*////////////////
#ifndef __MYFUNC_H
#define __MYFUNC_H
#ifdef _MYFUNC_IS_CPP
extern "C" void myfunction(int x, .....);
#else
void myfunction(int x, .....);
#endif
#endif
/////////////*myfunc.cpp*///////////////
#define _MYFUNC_IS_CPP
#include "myfunc.h"
extern "C" void myfunction(int x,...){
//Function body, can have C++ library calls (e.g. openCV) etc...
}
#undef _MYFUNC_IS_CPP
|
clang++ -c -g -Wall myfunc.cpp -o myfunc.o -I/path/to/opencv/include -I/path/to/myfunc
ar libmyfunc.a myfunc.o
ranlib libmyfunc.a
Now the issue is how to link this wrapper library to a C code: suppose that you have a C driver that makes use of myfunction, say something like this
1 2 3 4 5 6 7 8 9 10 11 12 13 | ///////////*driver.c*///////////////////
#include "myfunc.h"
int main(){
int x;
......
myfunction(x,....);
return 0;
}
|
clang -c -g -Wall driver.c -o driver.o -I/path/to/myfunc
At linking stage you need to specify the original C++ library you wanted to link to (e.g. openCV), as well as the standard C++ library, libstdc++
clang -g driver.o -o driver -L/path/to/myfunc -lmyfunc -L/path/to/opencv/lib -lopencv_core -lstdc++
And you are done!!