“multiple definition of `memcmp” error when linking Rust staticlib with embedded C program

前端 未结 1 1118
小蘑菇
小蘑菇 2021-01-20 01:21

I have a Rust function I would like to call from a C project that runs on an STM32F412 MCU, but I am getting a series of \"multiple definition of\" linker errors.

He

1条回答
  •  梦毁少年i
    2021-01-20 02:10

    You have a bunch of duplicated symbols coming from your customized "standard library" liba and from generated builtin symbols inserted into librust.a:

    memset, memcpy, memmove, ecc, ecc
    

    Your problem arises because the order of object files matters when linking.

    If you put librust.a too early in the ordered sequence of files to link, then files before librust.a will resolve symbols from librust.a and files coming after librust.a will resolve the same symbols from liba and this generates duplicate symbol errors.

    To avoid this problem, put the Rust library at the end of object files to link.

    In epsilon Makefile change the link commands as:

    RUST_LIB_DIR = 
    
    .SECONDARY: $(objs)
    %.$(EXE):
        @echo "LD      $@"
        $(Q) $(LD) $^ $(LDFLAGS) -L$(RUST_LIB_DIR) -l:librust.a -o $@ 
    

    This recipe links successfully on my side. My basic epsilon/apps/main.cpp instrumentation:

    #include "global_preferences.h"
    #include "apps_container_storage.h"
    
    extern "C" int hello_world(int a, int b);
    
    void ion_main(int argc, char * argv[]) {
    
      hello_world(1,2);
      ...
    

    0 讨论(0)
提交回复
热议问题