文章

OSG的CMAKE配置

1.引言

想写这个程序的起因是我们即将要开发三维CAD相关,所以用到OSG,但是OSG的安装过于繁琐,我们的项目下个月就要开始了,所以想着自己写个程序帮助同事们直接跳过最繁琐的配置环节,今天虽然是周六并且我还在发烧,但中午仍然是干了4个小时,写出来了这个程序,现在只把源码放上来,挖坑以后再填,具体的使用方法请看这里:OSG源码下载后,快速配置Cmake的C++程序

2.源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <filesystem>
#include <string>
#include <cstdlib>
#include <fstream>

namespace fs = std::filesystem;

int main() {
    // 第一步
    fs::path current_path = fs::current_path();
    fs::path third_party_x64_path = current_path / "3rdParty_x64";
    fs::path x64_path = third_party_x64_path / "x64";
    fs::path third_party_path = current_path / "3rdParty";
    fs::rename(x64_path, third_party_path);
    fs::remove_all(third_party_x64_path);

    fs::path osg_old_path = current_path / "OpenSceneGraph-OpenSceneGraph-3.6.5";
    fs::path osg_new_path = current_path / "OpenSceneGraph";
    fs::rename(osg_old_path, osg_new_path);

    // 第二步
    fs::path build_path = osg_new_path / "build";
    fs::create_directory(build_path);

    std::string cmake_command = "cmake -S " + osg_new_path.string() + " -B " + build_path.string() +
        " -G \"Visual Studio 17 2022\" -A x64" +
        " -DACTUAL_3RDPARTY_DIR=" + third_party_path.string() +
        " -DBUILD_OSG_EXAMPLES=ON" +
        " -DCMAKE_INSTALL_PREFIX=" + osg_new_path.string() +
        " -DBUILD_MFC_EXAMPLE=ON";

    int result_configure = system(cmake_command.c_str());

    // 在桌面上创建zyh.txt文件并写入内容
    std::string desktop_path = std::getenv("USERPROFILE");
    desktop_path += "\\Desktop\\zyh.txt";

    std::ofstream file(desktop_path);

    if (result_configure == 0) {
        file << "没有红色提示" << std::endl;
        std::cout << "配置成功" << std::endl;
        file << "Generate成功" << std::endl;
        std::cout << "生成成功" << std::endl;
    }
    else {
        file << "有红色提示" << std::endl;
        std::cout << "配置失败" << std::endl;
        file << "Generate失败" << std::endl;
        std::cout << "生成失败" << std::endl;
    }

    file.close();
}
本文由作者按照 CC BY 4.0 进行授权