You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用单条Ubuntu Shell命令编译运行C++代码并调用gnuplot绘图

Hey Andrea, let's tackle this problem step by step—you're absolutely right to want to streamline this workflow, and it's easier than you might think!

一键完成编译、运行与绘图的Shell脚本方案

You can bundle all four steps into a single shell script that handles compilation, execution, and plotting automatically. Below is a complete, customizable example tailored to your needs:

#!/bin/bash

# 1. Compile your C++ code (adjust filenames to match your project)
echo "Compiling source code..."
g++ your_program.cpp -o compiled_program

# Check if compilation succeeded before proceeding
if [ $? -ne 0 ]; then
    echo "❌ Compilation failed! Exiting."
    exit 1
fi

# 2. Run the compiled program
echo "Running your program..."
./compiled_program

# Check if program execution worked
if [ $? -ne 0 ]; then
    echo "❌ Program crashed! Exiting."
    exit 1
fi

# 3. Launch gnuplot and plot your data
echo "Generating plot with gnuplot..."
# Pass plotting commands directly to gnuplot using the -e flag
gnuplot -e "
set title 'Your Program Output'
set xlabel 'X Values'
set ylabel 'Y Values'
set grid on  # Add grid for readability
plot 'data_output.txt' with lines points  # Adjust data filename and style as needed
pause -1  # Keep plot window open until you close it manually
"

How to use this script:

  • Save the code above as build_run_plot.sh
  • Give it execution permissions: chmod +x build_run_plot.sh
  • Run it anytime with: ./build_run_plot.sh

Key gnuplot details you might have missed:

The -e flag is the secret sauce here—it lets you pass gnuplot commands directly from the shell, no separate config file required. If your plotting needs get more complex (e.g., multiple data series, custom colors), you can move the gnuplot code to a separate file like plot_config.gnuplot:

set title "Advanced Data Plot"
set style line 1 lw 2 lc rgb '#2E86AB'
plot 'data_output.txt' using 1:2 title 'Series A' with linespoints ls 1, \
     'data_output.txt' using 1:3 title 'Series B' with linespoints ls 2
pause -1

Then update the script's gnuplot line to: gnuplot plot_config.gnuplot

  • Shell basics: Type man bash in the terminal to access the official bash manual, or help to get info on built-in commands like if and exit. Start with variables, conditionals, and script permissions.
  • Gnuplot fundamentals: Type gnuplot to enter interactive mode, then help to browse built-in documentation. Try help plot to dive into plotting syntax—experiment with different styles (e.g., with lines, with points) to see what works for your data.
  • Script customization: Add command-line arguments to make the script flexible. For example, let it accept your source filename as input with ./build_run_plot.sh my_code.cpp—use $1 in the script to reference this argument (update the compile line to g++ $1 -o compiled_program).

内容的提问来源于stack exchange,提问作者Andrea Angeletti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:06:58