Cap'n Proto计算器示例编译报错:未定义main函数,求运行方法
Ah, I get it—you’ve successfully generated the interface code from the .capnp file, but that code only defines the data structures and RPC interfaces, not the actual program entry point (main function). That’s exactly why the linker is throwing that error. Let me walk you through getting this example up and running properly:
1. Grab the missing implementation files
The Cap'n Proto repo’s C++ samples directory includes two critical files you’re missing right now:
calculator-server.c++: The server implementation that hosts the calculator service logiccalculator-client.c++: The client that connects to the server and sends test calculation requests
Make sure you download these two files and place them in the same directory as your generated calculator.capnp.c++ and .h files.
2. Compile the server binary
Run this command to compile the server—it combines the generated interface code with the server’s actual implementation:
g++ -o calculator-server calculator.capnp.c++ calculator-server.c++ -lcapnp -lkj -lcapnp-rpc -lkj-async
3. Compile the client binary
Do the same for the client with this command:
g++ -o calculator-client calculator.capnp.c++ calculator-client.c++ -lcapnp -lkj -lcapnp-rpc -lkj-async
4. Run the calculator example
- First, start the server in one terminal window:
./calculator-server - Then, open a second terminal and launch the client:
./calculator-client
You’ll see the client send a series of calculation requests to the server, and the server will process them and send back results—you should see the output printed in the client terminal.
内容的提问来源于stack exchange,提问作者LeenA

