0

I want to use C code which depends on babeltrace2 in Rust.

For now I have in my main.rs, where prepare_graph is a custom C function:

#[link(name = "bt_util", kind = "static")]
extern "C" {
    fn prepare_graph( 
        ctf_directory: *const c_char,
        plugins_directory: *const c_char
        ) -> c_int;
}

with libbabeltrace/bt_util.c:

#include <babeltrace2/babeltrace.h>
#include "bt_util.h"
int prepare_graph(const char *ctf_directory, const char *plugins_directory) {
    bt_graph *graph = bt_graph_create(0);
    int error_code = 0;
    bt_plugin_find_all_from_dir_status ret;
    const bt_plugin_set *plugins;
    ...
}

it is built with the following build.rs:

extern crate cmake;

use cmake::Config;

fn main(){
    let babeltrace_path = Config::new("libbabeltrace").build();

    println!("cargo:rustc-link-search=native={}", babeltrace_path.display());
    println!("cargo:rustc-link-lib=static=bt_util");
}

and the libbabeltrace/CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)
project(LibBabeltrace C)

set(CMAKE_C_STANDARD 17)

include(ExternalProject)
include(FetchContent)

set(EXTERNAL_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/external)
include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)

ExternalProject_Add(babeltrace2
        GIT_REPOSITORY https://github.com/efficios/babeltrace.git
        GIT_TAG v2.0.4
        BUILD_IN_SOURCE 0
        WORKING_DIR ${CMAKE_CURRENT_SOURCE_DIR}/babeltrace2
        SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/babeltrace2
        CONFIGURE_COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/babeltrace2/bootstrap && BABELTRACE_DEBUG_MODE=1 BABELTRACE_DEV_MODE=1 BABELTRACE_MINIMAL_LOG_LEVEL=TRACE BABELTRACE_PLUGINS_DIR=${CMAKE_CURRENT_SOURCE_DIR}/babeltrace2/plugins ./configure --disable-man-pages --disable-glibtest --disable-doxygen-doc --disable-doxygen-html --prefix "${EXTERNAL_INSTALL_LOCATION}"
        BUILD_COMMAND ${MAKE_EXE}
        BUILD_ALWAYS FALSE
        )

add_custom_target(build_external
        DEPENDS babeltrace2
        )
add_library(bt_util STATIC src/bt_util.c)
install(TARGETS bt_util DESTINATION .)

As can be seen the libbabeltrace/bt_util.c depends on an external library (babeltrace2). This leads to a compilation error within the build.rs which is due to not being able to link the external library.

   Compiling ctf_reader v0.1.0 (/home/tk/Documents/bitaggregat/minimal-stide-examples/Rust)
error: linking with `cc` failed: exit status: 1
  |

   ...

  = note: /usr/bin/ld: .../target/debug/build/ctf_reader-13cbb68ba72803ed/out/libbt_util.a(bt_util.c.o): in function `my_consumer_func':
          .../libbabeltrace/src/bt_util.c:18: undefined reference to `bt_message_get_type'
          /usr/bin/ld: .../libbabeltrace/src/bt_util.c:24: undefined reference to `bt_message_event_borrow_event_const'
          /usr/bin/ld: .../libbabeltrace/src/bt_util.c:25: undefined reference to `bt_event_borrow_class_const'
          /usr/bin/ld: .../libbabeltrace/src/bt_util.c:29: undefined reference to `bt_event_borrow_payload_field_const'

   ...

error: could not compile `ctf_reader` due to previous error

Couldn't find resources on including C with dependencies. I know there is also a babeltrace2-sys crate. But I couldn't get this to work with system call traces.

Any help appreciated.

  • Add `println!("cargo:rustc-link-lib=dylib=babeltrace2");` _after_ `bt_util` in your `build.rs`. – Jmb Jan 03 '23 at 10:10
  • Thanks that got me a step forward. It still does not work and I adjusted my entry. Is the weird debugging behavior maybe due to loading babeltrace dynamically? – HaralToepfer Jan 04 '23 at 08:01
  • Please don't edit questions to change them once they are resolved. If you have new issues once the first one is resolved, you should open a new question instead. – Jmb Jan 04 '23 at 08:09
  • Random jumps when debugging can occur either because the code was compiled with optimization, or because of stack corruption. It has nothing to do with dynamic libraries. – Jmb Jan 04 '23 at 08:11
  • Changed the question back to its original state. Thanks. – HaralToepfer Jan 04 '23 at 08:39

0 Answers0