Link fail with c program

I have tried: in rust_project\src have file c_program.c
cd rust_project and in main.rs have code

extern crate libc;
use libc::{c_double, c_int, size_t};
#[link(name = "c_program")]
//#[path="./src/c_program.c"]
extern "C" {
    fn c_program(Fs:*mut c_double, Nmax: c_int, smooth_intensity: c_double, Fi: *mut c_double, Ftd: *mut c_double) ->  c_int;
}
fn main(){
let smooth_intensity = 0.5;
let mut prediction = vec![0_f32; width];
let mut first_correction = vec![0_f32; width];
let mut second_correction = vec![0_f32; width];
 unsafe{  
c_program(inner_vector.as_mut_ptr() as *mut f64, all_steps as i32, smooth_intensity, 
first_correction.as_mut_ptr() as *mut f64, second_correction.as_mut_ptr() as *mut f64);}
                        }
```error: linking with `link.exe` failed: exit code: 1181
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.27.29110\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" 
"/LIBPATH:C:\\Users\\2020\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\Users\\2020\\RUSTprojects\\trburcor\\target\\debug\\deps\\RUSTFirstOrderEquation.1716bj1wfv6z8cu1.rcgu.o" 
"C:\\Users\\2020\\RUSTprojects\\trburcor\\target\\debug\\deps\\RUSTFirstOrderEquation.1909bmgoc9t
//...
"C:\\Users\\2020\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcfg_if-b99d159579768184.rlib" 
"C:\\Users\\2020\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-937a75dfa4d935e4.rlib" "C:\\Users\\2020\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-30937137cb83abb7.rlib" 
"C:\\Users\\2020\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-d454e7de92167dc4.rlib" 
"C:\\Users\\2020\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-51061a7c11411681.rlib" 
"C:\\Users\\2020\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-d0c7e844decdcc15.rlib" "kernel32.lib" "advapi32.lib" "cfgmgr32.lib" "gdi32.lib" "kernel32.lib" "msimg32.lib" "opengl32.lib" "user32.lib" "winspool.lib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"

  = note: Non-UTF-8 output: LINK : fatal error LNK1181: \xad\xa5 \xe3\xa4\xa0\xa5\xe2\xe1\xef \xae\xe2\xaa\xe0\xeb\xe2\xec \xa2\xe5\xae\xa4\xad\xae\xa9 \xe4\xa0\xa9\xab \".\\src\\smooth.c.lib\"\r\n
error: aborting due to previous error; 88 warnings emitted

error: could not compile `RUSTprogram`
To learn more, run the command again with --verbose.```
```
else if i place c_program.c on ..\ and rename c_program.c.dll then error:
error: linking with `link.exe` failed: exit code: 1107
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.27.29110\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\2020\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" 
//.....
"C:\\Users\\2020\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-51061a7c11411681.rlib" "C:\\Users\\2020\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-d0c7e844decdcc15.rlib" "kernel32.lib" "advapi32.lib" "cfgmgr32.lib" "gdi32.lib" "kernel32.lib" "msimg32.lib" "opengl32.lib" "user32.lib" "winspool.lib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"
 = note: Non-UTF-8 output: smooth.lib : fatal error LNK1107: \xad\xa5\xa4\xae\xaf\xe3\xe1\xe2\xa8\xac\xeb\xa9 \xa8\xab\xa8 \xaf\xae\xa2\xe0\xa5\xa6\xa4\xa5\xad\xad\xeb\xa9 \xe4\xa0\xa9\xab: \xad\xa5 \xe3\xa4\xa0\xa5\xe2\xe1\xef \xaf\xe0\xae\xe7\xa8\xe2\xa0\xe2\xec \xaf\xae 0x811\r\n ```

c_program.c needs to be compiled first. You can either compile it manually or use something like the cc crate as a build-dependency:

// Cargo.toml
[build-dependencies]
cc = "1.0"

Then in a build.rs file in your project root:

// build.rs
fn main() {
    cc::Build::new()
        .file("src/c_program.c")
        .compile("c_program");
}

You can then remove the #[link(name = "c_program")] from your main.rs

So to compile it what i need if i want to use libc?And is it necessary to indicate path from current directory to c_program in #link()?
And also, need i specify in program.c types as c_int, c_double or only in extern block?
I had done as above:
error: linking with link.exe failed: exit code: 1120
|
= note: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\Users\2020\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib"
//...
ib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"
= note: Non-UTF-8 output: Rustproject.uuyd5p4eumgtnk9.rcgu.o : error LNK2019: \xe1\xe1\xeb\xab\xaa\xa0 \xad\xa0 \xad\xa5\xe0\xa0\xa7\xe0\xa5\xe8\xa5\xad\xad\xeb\xa9 \xa2\xad\xa5\xe8\xad\xa8\xa9 \xe1\xa8\xac\xa2\xae\xab Smooth_Array_Zhmakin_Fursenko \xa2 \xe4\xe3\xad\xaa\xe6\xa8\xa8 _ZN49Rustproject4main17h3b90a45d36836028E.\r\nC:\Users\2020\RUSTprojects\trburcor\target\debug\deps\Rustproject.exe : fatal error LNK1120: \xad\xa5\xe0\xa0\xa7\xe0\xa5\xe8\xa5\xad\xad\xeb\xe5 \xa2\xad\xa5\xe8\xad\xa8\xe5 \xed\xab\xa5\xac\xa5\xad\xe2\xae\xa2: 1\r\n

error: aborting due to previous error; 88 warnings emitted

error: could not compile Rustproject

Could you please put compiler errors inside code blocks, like with Rust code? You can start the block with ```text to stop it from being syntax-highlighted.

I’m not sure what the error is. Is your C file UTF-8 encoded. Is it the only C file. Can you create a github repo where we can take a look.

1 Like

Also i had commented extern "c" and further uses, then compiler return new error:
error occurred: Environment variable OUT_DIR not defined.
what is it?
I had tried:

use std::env;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
println!("{}", out_dir);
}
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NotPresent', src\main.rs:25:35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\RUSTDraft.exe main.rs` (exit code: 101)

@Maxim did you put the code from Link fail with c program - #2 by MoAlyousef in build.rs next to the Cargo.toml file?

1 Like

Okay, so: i had tried on windows and linux and had found possible problem:
On linux error:
error: linking with cc failed: exit status: 1
|
= note: "cc" "-m64" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack"
//.....
= note: /usr/bin/ld: cannot find -lsmooth
collect2: error: ld returned 1 exit status
error: aborting due to previous error; 5 warnings emitted
error: could not compile RUSTDraft
And found explanation:

`ld returned 1 exit status` обычно означает, что у Вас в коде есть недопустимые символы. Они могут там быть даже если их вроде и не видно, например, русская буква `е` от английской `e` на первый взгляд ничем не отличается. Обычно это результат копипасты. Так как функция небольшая, то самый простой вариант для Вас решить проблему - переписать ее (вернее, **всю** копипасту) руками.

which means that in program there are non valid symbols, as above on windows were error non UTF 8 symbols, on linux ld returned 1 exit status.Maybe i will find it, although i had wreriten code and the the problem doesn't dissapear.
So also two problems:
if i enter in terminal: rustc --print native-static-libs --crate-type staticlib src/smooth.c
error: expected one of ! or ::, found c_func
--> src/smooth.c:2:5
|
2 | int c_func(double Fs, int Nmax, double smooth_in...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected one of ! or ::
error: aborting due to previous error
And also error in main.rs if i will comment unsafe block(that is above on repo):
CARGO = "/home/computadormaxim/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo"
CARGO_HOME = "/home/computadormaxim/.cargo"
CARGO_MANIFEST_DIR = "/home/computadormaxim/RUSTprojects/Draft"
CARGO_PKG_AUTHORS = "Maximka-hue 53125181+Maximka-hue@users.noreply.github.com"
CARGO_PKG_DESCRIPTION = ""
CARGO_PKG_HOMEPAGE = ""
CARGO_PKG_LICENSE = ""
CARGO_PKG_LICENSE_FILE = ""
CARGO_PKG_NAME = "RUSTDraft"
CARGO_PKG_REPOSITORY = ""
CARGO_PKG_VERSION = "0.1.0"
CARGO_PKG_VERSION_MAJOR = "0"
CARGO_PKG_VERSION_MINOR = "1"
CARGO_PKG_VERSION_PATCH = "0"
CARGO_PKG_VERSION_PRE = ""
COLORTERM = "truecolor"
DBUS_SESSION_BUS_ADDRESS = "unix:path=/run/user/1000/bus"
DESKTOP_SESSION = "ubuntu"
DISPLAY = ":0"
GDMSESSION = "ubuntu"
GJS_DEBUG_OUTPUT = "stderr"
GJS_DEBUG_TOPICS = "JS ERROR;JS LOG"
GNOME_DESKTOP_SESSION_ID = "this-is-deprecated"
GNOME_SHELL_SESSION_MODE = "ubuntu"
GNOME_TERMINAL_SCREEN = "/org/gnome/Terminal/screen/c5936e26_fb6c_498b_bb5a_e89fc842f439"
GNOME_TERMINAL_SERVICE = ":1.115"
GPG_AGENT_INFO = "/run/user/1000/gnupg/S.gpg-agent:0:1"
GTK_MODULES = "gail:atk-bridge"
HOME = "/home/computadormaxim"
IM_CONFIG_PHASE = "1"
LANG = "en_US.UTF-8"
LANGUAGE = "en"
LC_ADDRESS = "ru_RU.UTF-8"
LC_IDENTIFICATION = "ru_RU.UTF-8"
LC_MEASUREMENT = "ru_RU.UTF-8"
LC_MONETARY = "ru_RU.UTF-8"
LC_NAME = "ru_RU.UTF-8"
LC_NUMERIC = "ru_RU.UTF-8"
LC_PAPER = "ru_RU.UTF-8"
LC_TELEPHONE = "ru_RU.UTF-8"
LC_TIME = "ru_RU.UTF-8"
LD_LIBRARY_PATH = "/home/computadormaxim/RUSTprojects/Draft/target/debug/deps:/home/computadormaxim/RUSTprojects/Draft/target/debug:/home/computadormaxim/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib:/home/computadormaxim/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib"
LESSCLOSE = "/usr/bin/lesspipe %s %s"
LESSOPEN = "| /usr/bin/lesspipe %s"
LOGNAME = "computadormaxim"
LS_COLORS = "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:
.tar=01;31:.tgz=01;31:.arc=01;31:.arj=01;31:.taz=01;31:.lha=01;31:.lz4=01;31:.lzh=01;31:.lzma=01;31:.tlz=01;31:.txz=01;31:.tzo=01;31:.t7z=01;31:.zip=01;31:.z=01;31:.dz=01;31:.gz=01;31:.lrz=01;31:.lz=01;31:.lzo=01;31:.xz=01;31:.zst=01;31:.tzst=01;31:.bz2=01;31:.bz=01;31:.tbz=01;31:.tbz2=01;31:.tz=01;31:.deb=01;31:.rpm=01;31:.jar=01;31:.war=01;31:.ear=01;31:.sar=01;31:.rar=01;31:.alz=01;31:.ace=01;31:.zoo=01;31:.cpio=01;31:.7z=01;31:.rz=01;31:.cab=01;31:.wim=01;31:.swm=01;31:.dwm=01;31:.esd=01;31:.jpg=01;35:.jpeg=01;35:.mjpg=01;35:.mjpeg=01;35:.gif=01;35:.bmp=01;35:.pbm=01;35:.pgm=01;35:.ppm=01;35:.tga=01;35:.xbm=01;35:.xpm=01;35:.tif=01;35:.tiff=01;35:.png=01;35:.svg=01;35:.svgz=01;35:.mng=01;35:.pcx=01;35:.mov=01;35:.mpg=01;35:.mpeg=01;35:.m2v=01;35:.mkv=01;35:.webm=01;35:.webp=01;35:.ogm=01;35:.mp4=01;35:.m4v=01;35:.mp4v=01;35:.vob=01;35:.qt=01;35:.nuv=01;35:.wmv=01;35:.asf=01;35:.rm=01;35:.rmvb=01;35:.flc=01;35:.avi=01;35:.fli=01;35:.flv=01;35:.gl=01;35:.dl=01;35:.xcf=01;35:.xwd=01;35:.yuv=01;35:.cgm=01;35:.emf=01;35:.ogv=01;35:.ogx=01;35:.aac=00;36:.au=00;36:.flac=00;36:.m4a=00;36:.mid=00;36:.midi=00;36:.mka=00;36:.mp3=00;36:.mpc=00;36:.ogg=00;36:.ra=00;36:.wav=00;36:.oga=00;36:.opus=00;36:.spx=00;36:*.xspf=00;36:"
OLDPWD = "/home/computadormaxim/RUSTprojects"
PAPERSIZE = "a4"
PATH = "/home/computadormaxim/.cargo/bin:/home/computadormaxim/.cargo/bin:/home/computadormaxim/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
PWD = "/home/computadormaxim/RUSTprojects/Draft"
QT_ACCESSIBILITY = "1"
QT_IM_MODULE = "ibus"
RUSTUP_HOME = "/home/computadormaxim/.rustup"
RUSTUP_TOOLCHAIN = "nightly-x86_64-unknown-linux-gnu"
RUST_RECURSION_COUNT = "1"
SESSION_MANAGER = "local/computadormaxim-HP-ENVY-15-Notebook-PC:@/tmp/.ICE-unix/1735,unix/computadormaxim-HP-ENVY-15-Notebook-PC:/tmp/.ICE-unix/1735"
SHELL = "/bin/bash"
SHLVL = "1"
SSH_AGENT_PID = "1691"
SSH_AUTH_SOCK = "/run/user/1000/keyring/ssh"
SSL_CERT_DIR = "/usr/lib/ssl/certs"
SSL_CERT_FILE = "/usr/lib/ssl/certs/ca-certificates.crt"
TERM = "xterm-256color"
USER = "computadormaxim"
USERNAME = "computadormaxim"
VTE_VERSION = "6200"
WINDOWPATH = "2"
XAUTHORITY = "/run/user/1000/gdm/Xauthority"
XDG_CONFIG_DIRS = "/etc/xdg/xdg-ubuntu:/etc/xdg"
XDG_CURRENT_DESKTOP = "ubuntu:GNOME"
XDG_DATA_DIRS = "/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop"
XDG_MENU_PREFIX = "gnome-"
XDG_RUNTIME_DIR = "/run/user/1000"
XDG_SESSION_CLASS = "user"
XDG_SESSION_DESKTOP = "ubuntu"
XDG_SESSION_TYPE = "x11"
XMODIFIERS = "@im=ibus"
_ = "/home/computadormaxim/.cargo/bin/cargo"

error occurred: Environment variable OUT_DIR not defined.

and also i had added function with nothing and get from rust:
/home/computadormaxim/RUSTprojects/Draft/src/main.rs:36: undefined reference to `callback'
collect2: error: ld returned 1 exit status

What to do with this?
Thanks

I've created a pull request to that repo that fixes the build. You can run the build using

cargo run

P.S. I've added some missing variables and gibberish values which were omitted in the question.

1 Like

Thanks a lot, simply i didn't use ffi before)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.