Skip to content

error: extern blocks must be unsafe #4510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
paulclem1 opened this issue May 8, 2025 · 1 comment
Open

error: extern blocks must be unsafe #4510

paulclem1 opened this issue May 8, 2025 · 1 comment
Labels

Comments

@paulclem1
Copy link

Describe the Bug

compiler reports "error: extern blocks must be unsafe" on compiling the following
extern "C" {

     fn c_function_name(x: i32) -> i32; 
     //fn c_function_name();
 } 

Even when wrapped in an unsafe loop, the code won't compile. The compiler responds with "error: expected item, found keyword unsafe"
if I wrap the extern C with "unsafe" as directed by the compiler

A clear and concise description of what you expected to happen:

Code should compile with no unsafe wrapping, and, as Ive stated above, compiler responds with a contradictory error if i do exactly what it asks.

I need to implement the extern C to overcome a cc error reported by the compiler. I'm in the process of writing code using the egui_macroquad crate to generate an oscilloscope trace. I'm using example code generated by AI. When I used egui_plot I had no cc error.
Here is the full AI generated example code
use macroquad::prelude::*;
use egui_macroquad;
use std::env;
use std::path::PathBuf;
use std::ffi::CString;
//use cc;
//use macroquad::ui::widgets::Window;
use egui::{Context, RichText, Window};
unsafe {
extern "C" {

     fn c_function_name(x: i32) -> i32; // Replace with your C function
     //fn c_function_name();
 }  }

#[macroquad::main("egui with macroquad")]
async fn main() {
unsafe {
let result = c_function_name(5); // Call your C function
// Use the result in your Macroquad code
}
let mut ctx = Context::default();
loop {
clear_background(WHITE);

    // Process keys, mouse, etc. (your normal game logic)

    // Call egui_macroquad::ui to create and interact with the egui UI
    egui_macroquad::ui(|egui_ctx| {
       // egui_macroquad::Window::new("My UI").show(egui_ctx, |ui| {
       egui::Window::new("My UI").open(&mut true).show(&ctx, |ui| {         
        //Window::new("My UI").show(egui_ctx, |ui| {        
            ui.label("This is an egui window!");
        });
    });

    // Draw things before egui (your normal Macroquad drawing)

    // Call egui_macroquad::draw to render the egui UI
    egui_macroquad::draw();
    /*unsafe {
         let result = c_function_name(10);
         println!("Result from C: {}", result);
     }  */
@paulclem1 paulclem1 added the bug label May 8, 2025
@shniubobo
Copy link

Have you read this guide and this rust book chapter? You can find examples there on how to use unsafe.

Even when wrapped in an unsafe loop, the code won't compile. The compiler responds with "error: expected item, found keyword unsafe"

What do you mean by wrapping a function declaration in a loop? That compiler error means you have written something syntactically wrong. Your problem should be solved just by turning extern "C" into unsafe extern "C".

Code should compile with no unsafe wrapping

Not in Rust 2024. See the linked guide above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants