0

I am working on a Solana program and facing issues building my project. When I run cargo build, the build completes successfully but doesn't generate the required .so file for deployment. Additionally, running cargo build-sbf or anchor build results in errors. Here is a summary of my environment, the steps I've taken, and the specific errors:

Environment:

  • macOS on M1 (ARM architecture)
  • Using rustup for Rust toolchain management
  • Installed Solana CLI

Steps Taken:

  1. Install Rust toolchains:
    rustup install stable
    rustup install nightly
    rustup component add rust-src --toolchain nightly
    

Doesn't work : rustup target add bpfel-unknown-unknown

  1. Install Solana CLI:

    sh -c "$(curl -sSfL https://release.solana.com/stable/install)"
    
  2. Project Configuration:

    • Cargo.toml in the project root:

      [workspace]
      members = [
          "programs/*",
          "tests"
      ]
      
      [profile.release]
      lto = "fat"
      opt-level = "z"
      
    • Cargo.toml in programs/my_solana_program:

      [package]
      name = "my_solana_program"
      version = "0.1.0"
      edition = "2018"
      
      [dependencies]
      anchor-lang = "0.24.2"
      
      [lib]
      name = "my_solana_program"
      path = "src/lib.rs"
      crate-type = ["cdylib", "lib"]
      
  3. Attempt to build the program:

    cargo build
    

lib.rs

use anchor_lang::prelude::*;

declare_id!("5nXuGtAsawdDPnKPKeHGSdh2AXyNRFcjDTz9ACj5xPQs");

#[program]
pub mod my_solana_program {
    use super::*;

    pub fn process_transaction(_ctx: Context<ProcessTransaction>, transaction_type: String) -> Result<()> {
        msg!("Processing transaction of type: {}", transaction_type);
        Ok(())
    }
}

#[derive(Accounts)]
pub struct ProcessTransaction<'info> {
    #[account(mut)]
    pub signer: Signer<'info>,
}

Issues:

  • cargo build works but does not generate the .so file.
  • cargo build-sbf and anchor build fail with the following errors:

Errors:

Running cargo build-sbf and anchor build results in:

error: target is not supported, for more information see: https://docs.rs/getrandom/#unsupported-targets
   --> src/lib.rs:267:9
    |
267 | /         compile_error!("\
268 | |             target is not supported, for more information see: \
269 | |             https://docs.rs/getrandom/#unsupported-targets\
270 | |         ");
    | |__________^

error[E0433]: failed to resolve: use of undeclared crate or module `imp`
   --> src/lib.rs:291:5
    |
291 |     imp::getrandom_inner(dest)
    |     ^^^ use of undeclared crate or module `imp`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` (lib) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...

What I've Tried:

  • Updating all dependencies with cargo update
  • Cleaning the project with cargo clean and anchor clean
  • Verifying the correct installation of Rust toolchains and targets

Question:

How can I correctly build my Solana program to generate the .so file?

Any help or guidance would be greatly appreciated. Thank you!

3 Answers 3

1

It seems like there might be something missing in what you've provided. The error says that getrandom cannot be used in Solana programs, but the program you've provided doesn't seem to use any rand dependency.

In general, you'll need to avoid certain dependencies when building on-chain programs, like randomness, file-system operations, or networking, so be sure to remove those.

More info on these restrictions at https://solana.com/docs/programs/lang-rust#restrictions

0

cargo build-bpf is deprecated and was removed in favour of cargo build-sbf, ref https://github.com/anza-xyz/agave/pull/2040

Make sure you have the latest version of the Solana CLI

sh -c "$(curl -sSfL https://release.anza.xyz/stable/install)"

It also appears that you're using a very old version of anchor.

Update to 0.29.0 or the latest one on it too

avm use 0.29.0

# or

avm use latest

Anchor installation guide https://www.anchor-lang.com/docs/installation#installing-using-anchor-version-manager-avm-recommended

0

i am actually facing same issue, mine doesn't generate an error, cargo build-bpf does nothing, same with anchor build, it does absolutely nothing , doesn't log any error , doesn't generate .so file, i have trying for days now, no solution, i have the latest solana cli, latest avm was giving me issues during installation, i opted to use 0.30.0, but no solution yet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.