Remix编译Solidity代码出现Pragma版本警告问题求助
Hey there! I see you're diving into open-source Solidity code and hitting a confusing warning in Remix 0.5.0—even though you’ve already added the pragma solidity ^0.5.0; line. Let’s break down what’s going on and get this sorted out.
Why This Warning Popped Up
The warning is meant to remind developers to explicitly lock in a compiler version to avoid future compatibility issues. Since your code already includes the correct pragma statement, the problem is almost certainly with Remix’s compiler configuration rather than your code itself. Here are the easiest fixes:
1. Match Remix’s Compiler Version to Your Pragma
- Head to the Compile tab in Remix (the checkmark icon on the left)
- Look for the Compiler dropdown at the top of the panel
- Make sure you select a version that fits your pragma: either
0.5.0exactly, or any0.5.xrelease (the^in^0.5.0allows all versions from 0.5.0 up to—but not including—0.6.0) - If you picked a version outside this range (like 0.4.x or 0.6.x), Remix will still throw the warning even with your pragma line.
2. Clear Remix’s Cache and Recompile
Sometimes cached compilation data can cause false warnings:
- In the Compile tab, find the Clean button (looks like a small broom)
- Click it to wipe cached data
- Recompile your contract with the correct version selected.
3. Double-Check Your Pragma Placement
Your code already has the right line, but just to confirm: ensure pragma solidity ^0.5.0; is the first non-comment line in your file. Comments (like the Etherscan verification note) don’t interfere with this, but stray whitespace or characters before it could cause issues. Your code looks spot-on here, but it’s worth a quick glance.
Your Code for Reference
/** *Submitted for verification at Etherscan.io on 2020-08-25 */ pragma solidity ^0.5.0; contract ERC20Interface { function totalSupply() public view returns (uint); function balanceOf(address tokenOwner) public view returns (uint balance); function allowance (address tokenOwner, address spender) public view returns (uint remaining); function transfer(address to, uint tokens) public returns (bool success); function approve(address spender, uint tokens) public returns (bool success); function transferFrom (address from, address to, uint tokens) public returns (bool success); event Transfer(address indexed from, address indexed to, uint tokens); event Approval(address indexed tokenOwner, address indexed spender, uint tokens); } contract SafeMath { function safeAdd(uint a, uint b) public pure returns (uint c) { c = a + b; require(c >= a); } function safeSub(uint a, uint b) public pure returns (uint c) { require(b <= a); c = a - b; } function safeMul(uint a, uint b) public pure returns (uint c) { c = a * b; require(a == 0 || c / a == b); } function safeDiv(uint a, uint b) public pure returns (uint c) { require(b > 0); c = a / b; } } contract MYTOKEN is ERC20Interface, SafeMath { string public name; string public symbol; uint8 public decimals; uint256 public _totalSupply; mapping(address => uint) balances; mapping(address => mapping(address => uint)) allowed; constructor() public { name = "MYTOKEN"; symbol = "MYT"; decimals = 18; _totalSupply = 1000000000000000000000000; balances[msg.sender] = _totalSupply; emit Transfer(address(0), msg.sender, _totalSupply); } function totalSupply() public view returns (uint) { return _totalSupply - balances[address(0)]; } function balanceOf(address tokenOwner) public view returns (uint balance) { return balances[tokenOwner]; } function allowance(address tokenOwner, address spender) public view returns (uint remaining) { return allowed[tokenOwner][spender]; } function approve(address spender, uint tokens) public returns (bool success) { allowed[msg.sender][spender] = tokens; emit Approval(msg.sender, spender, tokens); return true; } function transfer(address to, uint tokens) public returns (bool success) { balances[msg.sender] = safeSub(balances[msg.sender], tokens); balances[to] = safeAdd(balances[to], tokens); emit Transfer(msg.sender, to, tokens); return true; } function transferFrom(address from, address to, uint tokens) public returns (bool success) { balances[from] = safeSub(balances[from], tokens); allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens); balances[to] = safeAdd(balances[to], tokens); emit Transfer(from, to, tokens); return true; } }
Give these steps a try, and that warning should vanish. Let me know if you run into any other snags with your contract!
内容的提问来源于stack exchange,提问作者Arman khan




