简体   繁体   English

使用介子构建 C 项目:处理 3rd 方本地库的正确方法

[英]C project build with meson: Correct way to handle 3rd party local libraries

I wanted to build a project that's making use of the Criterion testing library and I'm using meson to build it all.我想构建一个使用Criterion测试库的项目,我正在使用介子来构建它。 I'm able to build my own code, a static library which relies on just standard c libraries.我能够构建自己的代码,一个仅依赖于标准 c 库的静态库。 The unit tests rely on #include <criterion/criterion.h> .单元测试依赖于#include <criterion/criterion.h> If I try to download the binary archive and include it in the meson.build file, I'm not able to sucessfully build the project.如果我尝试下载二进制存档并将其包含在 meson.build 文件中,我将无法成功构建项目。 I believe I'm not confuring meson correctly.我相信我没有正确地混淆介子。 I'm new to meson so I'm not sure what it is I'm getting wrong.我是介子的新手,所以我不确定我做错了什么。

meson.build ,介子构建

project('is_substring', 'c')

static_library(
    'is_substring',
    'is_substring.h',
)

executable(
    'test_is_substring',
    'is_substring.test.c',
    include_directories: include_directories(
        'libs/criterion-v2.3.3',
    ),
    #dependencies: [
    #    dependency('criterion')
    #]
)

The project structure is,项目结构是,


  ▸ builddir/
  ▸ libs/criterion-v2.3.3/
    is_substring.h
    is_substring.test.c
    meson.build

and the Criterion binary archive tar.bz2 has extracted into ./libs ,并且 Criterion 二进制存档 tar.bz2 已提取到./libs

  ▾ libs/criterion-v2.3.3/
    ▾ include/criterion/
      ▸ internal/
        abort.h
        alloc.h
        assert.h
        criterion.h
        event.h
        hooks.h
        logging.h
        options.h
        output.h
        parameterized.h
        redirect.h
        stats.h
        theories.h
        types.h
    ▾ lib/
        libcriterion.so
        libcriterion.so.3
        libcriterion.so.3.1.0
    ▾ share/pkgconfig/
        criterion.pc

is_substring.test.c , is_substring.test.c ,

#include "criterion/criterion.h"
#include <stdbool.h>
#include <stdio.h>

#include "is_substring.h"


Test(is_substring, is_substring)
{
    cr_assert(is_substring("aaa", "a") == true);
}

is_substring.h , is_substring.h ,

#include "stdbool.h"

bool is_substring(const char *fullstr, const char *substr)
{
    return true;
}

The error when I try to build is,我尝试构建时的错误是,

francium@4ab5198f934f:/mnt/c/brute-force/builddir$ ninja
[1/2] Compiling C object 'test_is_substring@exe/is_substring.test.c.o'.
FAILED: test_is_substring@exe/is_substring.test.c.o
cc -Itest_is_substring@exe -I. -I.. -I../libs/criterion-v2.3.3 -fdiagnostics-color=always -pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -g -MD -MQ 'test_is_substring@exe/is_substring.test.c.o' -MF 'test_is_substring@exe/is_substring.test.c.o.d' -o 'test_is_substring@exe/is_substring.test.c.o' -c ../is_substring.test.c
../is_substring.test.c:1:10: fatal error: criterion/criterion.h: No such file or directory
 #include <criterion/criterion.h>
          ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.
francium@4ab5198f934f:/mnt/c/brute-force/builddir$

As pointed out by @jonathan-leffler in the comments, this gcc command works, compiles and links correctly,正如@jonathan-leffler 在评论中指出的那样,这个 gcc 命令可以正确工作、编译和链接,

gcc -o is_substring.test is_substring.test.c -I libs/criterion-v2.3.3/include/ -Llibs/criterion-v2.3.3/lib -lcriterion

Managed to get it to compile, but since the Criterion library is a shared library and it's not installed globally, it doesn't work if you just try to run the compiled test executable.设法让它编译,但由于 Criterion 库是一个共享库并且它不是全局安装的,如果您只是尝试运行编译的测试可执行文件,它就不起作用。

But the fix to make it compile is adding in a,但是使它编译的修复方法是添加一个,

executable(
    ...
    objects: [
        'libs/criterion-v2.3.3/lib/libcriterion.so',
    ]

Note that the include_directories path should also be 'libs/criterion-v2.3.3/include/' with the include/ at the end.请注意, include_directories路径也应该是'libs/criterion-v2.3.3/include/' ,最后是include/

Full meson.build ,介子.build ,

project('is_substring', 'c')

static_library(
    'is_substring',
    'is_substring.h',
)

include_criterion = include_directories(
    'libs/criterion-v2.3.3/include/',
)

executable(
    'test_is_substring',
    'is_substring.test.c',
    include_directories: [
        include_criterion,
    ],
    objects: [
        'libs/criterion-v2.3.3/lib/libcriterion.so',
    ]
)

But running the output executable gives the following error at runtime,但是运行输出可执行文件在运行时会出现以下错误,

./test_is_substring: error while loading shared libraries: libcriterion.so.3: cannot open shared object file: No such file or directory

As suggested by Jonathan, fixed for this are,正如乔纳森所建议的,为此修复的是,

You may need to specify -R/opt/criterion-v2.3.3/lib or -R/usr/local/lib if you will install your Criterion library into one of those places.如果要将 Criterion 库安装到这些位置之一,则可能需要指定 -R/opt/criterion-v2.3.3/lib 或 -R/usr/local/lib。 Or that may be misremembered from Solaris and not relevant on Linux.或者在 Solaris 中可能会记错并且在 Linux 上不相关。 The system might look in /usr/local/lib anyway.无论如何,系统可能会在 /usr/local/lib 中查找。 There's also LD_LIBRARY_PATH;还有 LD_LIBRARY_PATH; you could add /home/you/project/libs/criterion-v2.3.3/lib (or thereabouts) to your LD_LIBRARY_PATH environment variable and that should allow it to be picked up until it is formally installed您可以将 /home/you/project/libs/criterion-v2.3.3/lib (或相关的)添加到您的 LD_LIBRARY_PATH 环境变量中,这应该允许它在正式安装之前被拾取

Thanks to @jonathan-leffler for his help and insight.感谢@jonathan-leffler 的帮助和洞察力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用正确的 dll 文件在 Cython C 扩展中启用第三方 C 库? - How do I use the correct dll files to enable 3rd party C libraries in a Cython C extension? 第三方图书馆定义/重新定义冲突 - 3rd party libraries conflicting definitions/ redefinitons 对使用开源第三方库感到困惑 - Confused in using opensource 3rd party libraries 我如何将第 3 方库包含到 python3 manylinux 构建中? - How can i include 3rd party libraries into a python3 manylinux build? 在不使用3rd party库的情况下基于C *中基于char *的多平台Unicode处理? - Multi-platform Unicode handling based on char* in C without using 3rd party libraries? 在C语言中,如何添加第三方库并使用Makefile从我现有的代码库中调用 - In C, how to add 3rd party Libraries and call from my existing code base using Makefile 使用gdb通过共享库进入第三方功能 - using gdb to step into 3rd party functions with shared libraries 如何为我的iPhone项目在Xcode中使用第三方C库? - How do I use a 3rd party C library in Xcode for my iphone project? 初学者将Crd Lion与3rd Party C文件一起使用 - Using 3rd Party C files with C Lion, a beginner perspective C ++单元测试和存根第三方C库 - C++ Unit Testing and stubbing a 3rd party C library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM