# 11.4 以Conda包的形式发布一个简单的项目

**NOTE**:*此示例代码可以在* <https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-11/recipe-04> *中找到。该示例在CMake 3.5版(或更高版本)中是有效的，并且已经在GNU/Linux、macOS和Windows上进行过测试。*

虽然PyPI是发布Python包的标准平台，但Anaconda (<https://anaconda.org> )更为可能更为流行，因为它不仅允许使用Python接口发布Python或混合项目，还允许对非Python项目进行打包和依赖关系管理。这个示例中，我们将为一个非常简单的C++示例项目准备一个Conda包，该项目使用CMake配置和构建，除了C++之外没有依赖关系。下一个示例中，我们将会来看看一个更复杂的Conda包。

## 准备工作

我们的目标是打包以下示例代码(`example.cpp`)：

```cpp
#include <iostream>
int main() {
    std::cout << "hello from your conda package!" << std::endl;
    return 0;
}
```

## 具体实施

1. `CMakeLists.txt`文件给出了最低版本要求、项目名称和支持的语言:

   ```
   cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
   project(recipe-04 LANGUAGES CXX)
   set(CMAKE_CXX_STANDARD 11)
   set(CMAKE_CXX_EXTENSIONS OFF)
   set(CMAKE_CXX_STANDARD_REQUIRED ON)
   ```
2. 使用`example.cpp`构建`hello-conda`可执行目标：

   ```
   add_executable(hello-conda "")
   target_sources(hello-conda
     PRIVATE
         example.cpp
     )
   ```
3. 使用`CMakeLists.txt`定义安装目标：

   ```
   nstall(
     TARGETS
         hello-conda
     DESTINATION
         bin
     )
   ```
4. 将在一个名为`meta.yaml`的文件中，对Conda包进行描述。我们将把它放在`conda-recipe`目录下，文件结构如下：

   ```
   .
   ├── CMakeLists.txt
   ├── conda-recipe
   │    └── meta.yaml
   └── example.cpp
   ```
5. `meta.yaml`包含如下内容：

   ```yaml
   package:
     name: conda-example-simple
     version: "0.0.0"

   source:
     path: .. /  # this can be changed to git-url

   build:
     number: 0
     binary_relocation: true
     script:
       - cmake -H. -Bbuild_conda -G "${CMAKE_GENERATOR}" -DCMAKE_INSTALL_PREFIX=${PREFIX} # [not win]
       - cmake -H. -Bbuild_conda -G "%CMAKE_GENERATOR%" -DCMAKE_INSTALL_PREFIX="%LIBRARY_PREFIX%" # [win]
       - cmake - -build build_conda - -target install

   requirements:
     build:
       - cmake >=3.5
       - { { compiler('cxx') } }

   about:
     home: http://www.example.com
     license: MIT
     summary: "Summary in here ..."
   ```
6. 现在来构建包：

   ```
   $ conda build conda-recipe
   ```
7. 过程中屏幕上看到大量输出，但是一旦构建完成，就可以对包进行安装。首先，在本地进行测试：

   ```
   $ conda install --use-local conda-example-simple
   ```
8. 现在准备测试安装包，打开一个新的终端(假设Anaconda处于激活状态)，并输入以下内容：

   ```
   $ hello-conda

   hello from your conda package!
   ```
9. 测试成功后，再移除包装：

   ```
   $ conda remove conda-example-simple
   ```

## 工作原理

`CMakeLists.txt`中，安装目标是这个示例的一个基本组件:

```
install(
  TARGETS
      hello-conda
  DESTINATION
      bin
  )
```

目标的二进制文件会安装到`${CMAKE_INSTALL_PREFIX}/bin`中。变量由Conda定义，并且构建步骤中定义在`meta.yaml`：

```yaml
build:
  number: 0
  binary_relocation: true
  script:
    - cmake -H. -Bbuild_conda -G "${CMAKE_GENERATOR}" -DCMAKE_INSTALL_PREFIX=${PREFIX} # [not win]
    - cmake -H. -Bbuild_conda -G "%CMAKE_GENERATOR%" -DCMAKE_INSTALL_PREFIX="%LIBRARY_PREFIX%" # [win]
    - cmake - -build build_conda - -target install
```

将安装目录设置为`${prefix}` (Conda的内部变量)，然后构建并安装项目。调用构建目录命名为`build_conda`的动机与前面的示例类似：特定的构建目录名可能已经命名为`build`。

## 更多信息

配置文件`meta.yaml`可为任何项目指定构建、测试和安装步骤。详情请参考官方文档：<https://conda.io/docs/user-guide/tasks/build-packages/define-metadata.html>

要将Conda包上传到Anaconda云，请遵循官方的Anaconda文档: <https://docs.anaconda.com/anaconda-cloud/user-guide/>

此外，也可以考虑将Miniconda，作为Anaconda的轻量级替代品：<https://conda.io/miniconda.html>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chenxiaowei.gitbook.io/cmake-cookbook/11.0-chinese/11.4-chinese.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
