declare_program!
Anchor 0.32.1 declare_program! macro generates Rust modules for external programs: instruction builders, account types, and CPI helpers from IDL JSON.
Search across all documentation pages
Anchor 0.32.1 declare_program! macro generates Rust modules for external programs: instruction builders, account types, and CPI helpers from IDL JSON.
declare_program!(other_program);
other_program::cpi::initialize(
CpiContext::new(
ctx.accounts.other_program.to_account_info(),
other_program::cpi::accounts::Initialize { /* ... */ },
),
)?;When to reach for this: Your program CPIs to another Anchor program with a published IDL.
use anchor_lang::prelude::*;
declare_program!(marketplace);
#[derive(Accounts)]
pub struct ListViaMarketplace<'info> {
pub marketplace_program: Program<'info, marketplace::program::Marketplace>,
#[account(mut)]
pub listing: Signer<'info>,
}
pub fn list(ctx: Context<ListViaMarketplace>, price: u64) -> Result<()> {
marketplace::cpi::list(
CpiContext::new(
ctx.accounts.marketplace_program.to_account_info(),
marketplace::cpi::accounts::List {
listing: ctx.accounts.listing.to_account_info(),
},
),
price,
)?;
Ok(())
}What this demonstrates:
Place marketplace.json IDL in workspace and reference in build config per Anchor 0.32 docs.
Generated account structs reduce CPI meta mistakes vs raw invoke.
| Alternative | Use When | Don't Use When |
|---|---|---|
| Manual invoke with discriminator bytes | Non-Anchor callees | IDL-available programs |
| Client-only composition | No on-chain CPI | Atomic program composition |
0.32.1 across advanced topics.
Large fixed layouts when Borsh CU too high.
Not easily; plan forward growth.
No; document separately.
Callee IDL JSON at compile time.
Yes with correct account metas.
Avoid different IDL per feature on mainnet.
LiteSVM, Surfpool, devnet simulation.
No; last resort.
See Related links for declare_program.
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.
Reviewed by Chris St. John·Last updated Jul 16, 2026