#!/bin/bash
OUTDIR=./bin/Debug
mkdir -p ${OUTDIR}
CXX=clang++
CCARGS="-Ishared-object -Wall"
LIBRARY="-shared -fPIC"
LDFLAGS="-L${OUTDIR}"
echo "building shared object"
${CXX} ${CCARGS} "./shared-object/shared-object.cpp" ${LIBRARY} -o ${OUTDIR}/libshared-objects.so.1.20160226.0
cd ${OUTDIR}
ln -sfn libshared-objects.so.1.20160226.0 libshared-objects.so.1
ln -sfn libshared-objects.so.1 libshared-objects.so
cd ../..
echo "building first app"
${CXX} ${CCARGS} "./first-app/first-app.cpp" -lshared-objects ${LDFLAGS} -o ${OUTDIR}/first-app.exe
echo "building second app"
${CXX} ${CCARGS} "./second-app/second-app.cpp" -lshared-objects ${LDFLAGS} -o ${OUTDIR}/second-app.exe
Library API (./shared-object/shared-object.h):
#ifndef __SHARED_OBJECT_H__
#define __SHARED_OBJECT_H__
extern "C" int calc(int a, int b);
#endif
Library implementation (./shared-object/shared-object.cpp):
#include "shared-object.h"
int calc(int a, int b)
{
return a+b;
}
First application (./first-app/first-app.cpp):
#include
#include "shared-object.h"
using namespace std;
int main(int argc, char** argv)
{
cout << "first-app " << calc (3,11) << endl;
return 0;
}
Second application (./second-app/second-app.cpp):
#include
#include "shared-object.h"
using namespace std;
int main(int argc, char** argv)
{
cout << "second-app " << calc(2,5) << endl;
return 0;
}