While you could find a way to link with Macaulay2 or some of the many libraries it uses, I would anticipate that to be tricky and fragile. A far easier task would be to use Macaulay2's programming language to establish some sort of inter-process communication with the C++ program you're writing.
For example, say you had the file "socket_server.m2" containing this:
someFunction = (a,b) -> a+b;
someRing=ZZ[p,q];
-- Server portion
listener=openListener("$:5000");
-- Block until a connection is received. Open the connection
conn=openInOut(listener);
stdio<<"Connection opened!"<<endl<<flush;
while (isOpen(conn) and not(atEndOfFile(conn))) do (
stdio<<"Waiting for a command."<<endl<<flush;
inString=read(conn);
-- inString has an EOL, but should be a single line.
inStringLines=lines(inString);
if not(#inStringLines==1) then (
stdio<<"Received "<<#inStringLines<<" lines of input; unexpected. Aborting"<<endl<<flush;
break;
);
inStringCmd=inStringLines#0;
stdio<<"Received \""<<inStringCmd<<"\". Will execute."<<endl<<flush;
outString=toString(value(inStringCmd));
stdio<<" Cmd evaluated to \""<<outString<<"\". Sending reply."<<endl<<flush;
conn<<outString<<endl<<flush;
)
stdio<<"Server finished with isOpen="<<isOpen(conn)<<" and atEndOfFile="<<atEndOfFile(conn)<<endl<<flush;
close(conn)
close(listener)