【问题标题】:How to convert a simple GCC command to CMake? [duplicate]如何将简单的 GCC 命令转换为 CMake? [复制]
【发布时间】:2020-08-30 17:42:32
【问题描述】:

我的源代码

#include <tree_sitter/api.h>
int main() {
    TSParser *parser = ts_parser_new();
}

我用下面的 gcc 命令编译它

gcc \
-I ~/source_repos/tree-sitter/lib/include \
test-json-parser.c \
~/source_repos/tree-sitter-json/src/parser.c \
~/source_repos/tree-sitter/libtree-sitter.a \
-o test-json-parser

这很好用。但是如果我在 CMake 中执行以下操作

cmake_minimum_required(VERSION 3.17)
project(tmp_tree_sitter C)

set(CMAKE_C_STANDARD 11)


include_directories(/home/rip/source_repos/tree-sitter/lib/include)

add_executable(tmp_tree_sitter
        main.c
        /home/rip/source_repos/tree-sitter/libtree-sitter.a
        /home/rip/source_repos/tree-sitter-json/src/parser.c
)

引发以下错误消息

Scanning dependencies of target tmp_tree_sitter
[ 33%] Building C object CMakeFiles/tmp_tree_sitter.dir/main.c.o
[ 66%] Linking C executable tmp_tree_sitter
/usr/bin/ld: CMakeFiles/tmp_tree_sitter.dir/main.c.o: in function `main':
/home/rip/CLionProjects/tmp_tree-sitter/main.c:12: undefined reference to `ts_parser_new'

唯一的区别是,我有不同的目录,所以一个源文件命名为test-json-parser.c,另一个命名为main.c。那么我如何在CMake 中做同样的事情,就像我对gcc 所做的那样?

【问题讨论】:

  • 我不知道是否应该发布错误消息,因为我还必须发布源代码,我认为这与我的问题无关,因为源代码是同样,这与 gcc 配合得很好。
  • “不起作用”并没有给我们很多工作。错误消息会有所帮助。
  • 好的,我添加了错误信息。
  • ~ 的扩展由 shell 完成。也许 CMake 不这样做。您是否尝试对 C 文件使用完整路径名?
  • 是的,我也试过这个。我将更新问题中的路径。

标签: c cmake


【解决方案1】:

你需要告诉 Cmake 你想要链接这些库。

我为此使用target_link_librarieshttps://cmake.org/cmake/help/latest/command/target_link_libraries.html

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 2021-10-09
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多