Hot questions for Using Cap'n Proto in linux

Question:

I think this is a generic problem not specifically related to Cap'n Proto.

My project's build succeeds but on run I get:

error while loading shared libraries: libcapnp-0.5.3.so: cannot open shared object file: No such file or directory

Maybe I am missing something in CMake?

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lcapnp -lkj")

I verified libcapnp-0.5.3.so exists in /usr/local/lib

I also tried adding:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L/usr/local/lib/libcapnp-0.5.3.so")

Answer:

/usr/local/lib needs to be added to /etc/ld.so.conf

then run sudo ldconfig to load the changes.

Question:

I am new to Cap'n Proto and I was trying to compile the calculator example found here: https://github.com/capnproto/capnproto/blob/master/c%2B%2B/samples/calculator.capnp

I used this command capnp compile -oc++ calculator.capnp to generate the calculator.capnp.c++ and calculator.capnp.h files.

I tried to compile the calculator.capnp.c++ file using the following command: g++ -o calc calculator.capnp.c++ -lcapnp -lkj -lcapnp-rpc -lkj-async but I received this error message:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function '_start': (.text+0x20): undefined reference to 'main' collect2: error: ld returned 1 exit status

I went over the official documentation and other resources, but I haven't found instructions on how to run this example (or anything similar). How can I proceed and run it?


Answer:

You're not building the whole sample, only the generated protocol files.

Notice how the repo also has calculator-server.c++ and calculator-client.c++, both of which contain the main entrypoint function. Pick one and link it into your program.

If you use the CMakeLists.txt already provided, this is done for you.