Rust call c function linking with `cc` failed: exit code: 1


#1

i use rust call c function,but is error,i am novice at rust.please help me how to fix that.
these is my code ant configure.
cargo.toml
[package]
name = "rusttest"
version = "0.1.0"
authors = [“744382100@qq.com”]
links="hello"
build = "build.rs"
linker = “gcc”

[build-dependencies]
gcc = "0.3"

[dependencies]
libc = "0.2"
gcc = "0.3"

build.rs

extern crate gcc;
fn main(){
    gcc::compile_library("libhello.a", &["src/c/hello.cc"]);
}         

hello.cc
#include <printf.h>

extern void helloworld(){
    char * hi = "hello";
    printf("%s",hi);
}

main.rs
extern crate libc;
#[link(name=“hello”, kind = “static”)]

extern {
    pub fn helloworld() -> libc::c_void;
}
fn main() {
        unsafe {helloworld()};
}

my os is macos sieera

when i run the error is

/Users/wangyiran/.cargo/bin/cargo run --color=always -v --bin rusttest
warning: unused manifest key: package.linker
Fresh libc v0.2.20
Fresh gcc v0.3.42
Compiling rusttest v0.1.0 (file:///Users/wangyiran/source/rusttest)
Running /Users/wangyiran/.cargo/bin/rustc build.rs --color always --crate-name build_script_build --crate-type bin -g -C metadata=9d205c9fcbea0045 --out-dir /Users/wangyiran/source/rusttest/target/debug/build/rusttest-9d205c9fcbea0045 --emit=dep-info,link -L dependency=/Users/wangyiran/source/rusttest/target/debug/deps --extern gcc=/Users/wangyiran/source/rusttest/target/debug/deps/libgcc-08d48d573059ea67.rlib
Running /Users/wangyiran/source/rusttest/target/debug/build/rusttest-9d205c9fcbea0045/build-script-build
warning: src/c/hello.cc:7:17: warning: conversion from string literal to ‘char *’ is deprecated [-Wc++11-compat-deprecated-writable-strings]
warning: char * hi = “hello”;
warning: ^
warning: 1 warning generated.
Running /Users/wangyiran/.cargo/bin/rustc src/main.rs --color always --crate-name rusttest --crate-type bin -g -C metadata=9d205c9fcbea0045 --out-dir /Users/wangyiran/source/rusttest/target/debug --emit=dep-info,link -L dependency=/Users/wangyiran/source/rusttest/target/debug/deps --extern gcc=/Users/wangyiran/source/rusttest/target/debug/deps/libgcc-08d48d573059ea67.rlib --extern libc=/Users/wangyiran/source/rusttest/target/debug/deps/liblibc-29ef97a68464c2b7.rlib -L native=/Users/wangyiran/source/rusttest/target/debug/build/rusttest-9d205c9fcbea0045/out -l static=hello
error: linking with cc failed: exit code: 1
|
= note: “cc” “-m64” “-L” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib” “/Users/wangyiran/source/rusttest/target/debug/rusttest.0.o” “-o” “/Users/wangyiran/source/rusttest/target/debug/rusttest” “-Wl,-dead_strip” “-nodefaultlibs” “-L” “/Users/wangyiran/source/rusttest/target/debug/deps” “-L” “/Users/wangyiran/source/rusttest/target/debug/build/rusttest-9d205c9fcbea0045/out” “-L” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib” “-Wl,-force_load,/Users/wangyiran/source/rusttest/target/debug/build/rusttest-9d205c9fcbea0045/out/libhello.a” “-Wl,-force_load,/Users/wangyiran/source/rusttest/target/debug/build/rusttest-9d205c9fcbea0045/out/libhello.a” “/Users/wangyiran/source/rusttest/target/debug/deps/liblibc-29ef97a68464c2b7.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-f5a209a9.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-f5a209a9.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-f5a209a9.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librand-f5a209a9.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcollections-f5a209a9.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_unicode-f5a209a9.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-f5a209a9.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc_jemalloc-f5a209a9.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-f5a209a9.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-f5a209a9.rlib” “/Users/wangyiran/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-f5a209a9.rlib” “-l” “System” “-l” “pthread” “-l” “c” “-l” “m”
= note: Undefined symbols for architecture x86_64:
"_helloworld", referenced from:
rusttest::main::hd8bef50835ef54f0 in rusttest.0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: aborting due to previous error

error: Could not compile rusttest.

Caused by:
process didn’t exit successfully: /Users/wangyiran/.cargo/bin/rustc src/main.rs --color always --crate-name rusttest --crate-type bin -g -C metadata=9d205c9fcbea0045 --out-dir /Users/wangyiran/source/rusttest/target/debug --emit=dep-info,link -L dependency=/Users/wangyiran/source/rusttest/target/debug/deps --extern gcc=/Users/wangyiran/source/rusttest/target/debug/deps/libgcc-08d48d573059ea67.rlib --extern libc=/Users/wangyiran/source/rusttest/target/debug/deps/liblibc-29ef97a68464c2b7.rlib -L native=/Users/wangyiran/source/rusttest/target/debug/build/rusttest-9d205c9fcbea0045/out -l static=hello (exit code: 101)

Process finished with exit code 101

how to fix that?thanks very much.


#2

You need to change this code

extern void helloworld(){
    char* hi = "hello";
    printf("%s",hi);
}

to

extern "C" void helloworld(){
    char* hi = "hello";
    printf("%s\n",hi);
}

#3

thanks very much!