如何用 Makefile 反向生成 CMakeLists 文件

如何用 Makefile 反向生成 CMakeLists 文件,第1张

CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性。只是 CMake 的组态档取名为 CmakeLists.txt。Cmake 并不直接建构出最终的软件,而是产生标准的建构档(如 linux 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),然后再依一般的建构方式使用。

在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下:

编写 CmakeLists.txt。

执行命令 “cmake PATH” 或者 “ccmake PATH” 生成 Makefile ( PATH 是 CMakeLists.txt 所在的目录 )。

使用 make 命令进行编译

工程实例:

一. 编写各层CMakeLists.txt

主目录的主程序main.cpp

#include "hello.h"

extern Hello hello

int main()

{

hello.Print()

return 0

}

主目录的CMakeLists.txt

# to the root binary directory of the project as ${MAIN_BINARY_DIR}.

project (MAIN)

#version support

cmake_minimum_required(VERSION 2.8)

# Recurse into the "Hello" and "Demo" subdirectories. This does not actually

# cause another cmake executable to run. The same process will walk through

# the project's entire directory structure.

add_subdirectory (Hello)

add_subdirectory (Demo)

# Make sure the compiler can find include files from our Hello library.

include_directories (${MAIN_SOURCE_DIR}/Hello)

# Make sure the linker can find the Hello Demo library once it is built.

link_directories (${HELLO_BINARY_DIR}/Hello)

link_directories (${HELLO_BINARY_DIR}/Demo)

#define the source coedes of current directory as DIR_SRCS

AUX_SOURCE_DIRECTORY(. DIR_SRCS)

# Add executable called "MAIN" that is built from the source files

add_executable (Main ${DIR_SRCS})

# Link the executable to the Hello Demo library.

target_link_libraries (Main Hello Demo)

定义项目名project(MAIN),使得当前目录可以用${MAIN_SOURCE_DIR},由于有2个子目录,所以需要add_subdirectory它们。由于主程序会使用到其他库,因而也需要指定连接库所在目录。

主目录下的作用是利用add_executable将当前目录下的源文件编译成Main程序,然后通过target_link_libraries链接Hello和Demo库。由于主程序文件使用了hello.h文件,所以要include_directories该目录。

---------------------------------------------------------------------------------------------------

子目录Demo的子程序demo.c

#include "hello.h"

Hello hello

子目录Demo的CMakeLists.txt

# Make sure the compiler can find include files from our Hello library.

include_directories (${MAIN_SOURCE_DIR}/Hello)

#define the source coedes of current directory as DIR_DEMO_SRCS

AUX_SOURCE_DIRECTORY(. DIR_DEMO_SRCS)

# Add library called "Demo" that is built from the source files

add_library (Demo ${DIR_DEMO_SRCS})

Demo目录下的CMakeLists主要作用是利用add_library将当前目录源码编译成Demo库,由于该库使用到hello.h文件,所以要include_directories该目录。

---------------------------------------------------------------------------------------------------

子目录Hello的子程序hello.h

#ifndef _hello_h

#define _hello_h

class Hello

{

public:

void Print()

}

#endif

子目录Hello的子程序hello.c

#include "hello.h"

#include <stdio.h>

void Hello::Print()

{

printf("Hello, World!\n")

}

子目录Hello的CMakeLists.txt

#define the source coedes of current directory as DIR_HELLO_SRCS

AUX_SOURCE_DIRECTORY(. DIR_HELLO_SRCS)

# Add library called "hello" that is built from the source files

add_library (Hello ${DIR_HELLO_SRCS})

Hello目录下的CMakeLists主要作用是利用add_library将当前目录源码编译成Hello库。

---------------------------------------------------------------------------------------------------

二. 执行cmake命令

至此我们完成了项目中所有 CMakeLists.txt 文件的编写,进入目录 step2 中依次执行命令

#cmake .

默认当前目录,生产makefile

#make

最后编译程序

可以。

还原数据库:打开SQL2000“企业管理器/数据库”,然后选中要还原到的数据库,右击,选择“所有任务/还原数据库”,在d出窗口中,选择“从设备”还原,再点击“选择设备”/“添加”,选择要还原的目标数据库,点击三次“确定”,切换至“选项”窗口,勾选“在现有数据库上强制还原”,并检查数据库还原路径是否正确,若路径不正确,可在此修改路径,最后再点击“确定”,提示数据库还原顺利完成即可。

新建数据库:点击“开始/程序”菜单,打开SQL2000“企业管理器”,展开“控制台根目录”下的“数据库”,选中“数据库”,右击,选择“新建数据库”,在d出窗口中输入“数据库名称”,点击“确定”即完成新建。

备份数据库:打开SQL2000“企业管理器/数据库”,选中需备份的数据库,右击,选择“所有任务/备份数据库”,在d出的备份窗口中,选择备份路径,并输入备份文件名称,点击“确定”,提示备份顺利完成即可。

用 Windows优化大师 ,里面有个智能软件卸载,通过它可以备份软件,包括所有的注册表信息,重装后通过Windows优化大师还原即可。

我试过帮人备份 股市分析 之类的软件,成功恢复。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11567169.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存