最终实现C++程序调用终端命令,运行sh脚本文件,通过sh文件激活虚拟环境。
运行成功的sh文件内容(命名test.sh)
# added by Archiconda3 0.2.2 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/xtark/archiconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
\eval "$__conda_setup"
else
if [ -f "/home/xtark/archiconda3/etc/profile.d/conda.sh" ]; then
. "/home/xtark/archiconda3/etc/profile.d/conda.sh"
CONDA_CHANGEPS1=false conda activate base
else
\export PATH="/home/xtark/archiconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda init <<<
conda activate f
C++程序调用终端命令
system("source test.sh);
遇到的一些问题:
sh: 1: source: not found
参考:sh: 1: source: not found on ubuntu_Jack的博客-CSDN博客
原因说明:
1、Ubuntu系统中,默认使用的shell是/bin/dash,它不支持source命令;
2、由于大多数shell都是使用的/bin/bash,因此简单粗暴,直接改变shell运行环境
解决:
sudo dpkg-reconfigure dash #选择No
显示:/bin/sh -> dash
sudo dpkg-reconfigure dash #选择No
ls -l `which sh`
显示:/bin/sh -> bash
conda:command not found
这个问题是因为我们刚开始的sh脚本文件只写了conda activate f
没有写前面的一些,我的理解是没有在脚本文件配置环境变量也是不行的,和以下问题类似。