【问题标题】:Bash script to compile and run multiple servers and clients c用于编译和运行多个服务器和客户端 c 的 Bash 脚本
【发布时间】:2013-09-17 07:01:14
【问题描述】:

我正在编写一个 C++ 客户端/服务器应用程序,它带有一个接受来自多个客户端的连接的多线程服务器。 我需要编写一个 bash 脚本来编译和执行服务器和客户端的多个实例。客户端和服务器位于不同的文件夹中。

我试过这种方式,但它不起作用,因为它只启动服务器:

#!/bin/sh
cd "/home/myhost/ServerSide"
g++ -std=c++11 -pthread server_struct.cpp -o server     
./server

cd "/home/myhost/ClientSide"
g++ -std=c++11 client.cpp -o client1
./client1

cd "/home/myhost/ClientSide"
g++ -std=c++11 client.cpp -o client2
./client2

【问题讨论】:

  • 为什么需要多次编译客户端可执行文件?一次编译启动多个实例还不够吗?

标签: c++ multithreading bash shell client-server


【解决方案1】:

通常我会将构建过程与运行您的应用程序分开。 此外,我建议使用make 进行构建,而不是使用 bash 脚本。

Makefile(注意缩进!):

CXXFLAGS=-std=c++11

VPATH=ServerSide ClientSide

all: client server

server: server_struct.o
    $(CXX) -o $@ $^ $(LDFLAGS)
client: client.o
    $(CXX) -o $@ $^ $(LDFLAGS)
runall: server client
    xterm -e ./server &
    xterm -e ./client &
    xterm -e ./client &

然后简单地运行make runall

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 2022-06-16
    • 2020-03-31
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多