Compiler version not recognized

Anyone have any ideas how to fix this?
Compiler version is there.

$ solc --optimize --abi ./contracts/MySmartContract.sol -o build
./contracts/MySmartContract.sol:1:1: Warning: Source file does not specify required compiler version! Consider adding “pragma solidity ^0.5.16;”

^
$ cat ./contracts/MySmartContract.sol
pragma solidity ^0.5.16;

contract MySmartContract {
function Hello() public view returns (string memory) {
return “Hello World”;
}
function Greet(string memory str) public view returns (string memory) {
return str;
}
}

$ solc --version
solc, the solidity compiler commandline interface
Version: 0.5.16+commit.9c3226ce.Linux.g++

I can’t reproduce this error even though I tried the exact compiler version you specified.

First, please put your code in proper code blocks (i.e. between ``` and ```). The forum mangles unquoted code so I’m not sure if the problem isn’t caused by something that’s no longer in your post. For example your code as shown above uses weird non-ASCII quote marks that are not valid in Solidity code and I’m assuming they were not there originally?

Second, let’s try something that should be easily reproducible. What happens if you paste this snippet into your shell? Does the compiler still complain about the pragma?

cd /tmp/
mkdir contracts/

cat << EOF > contracts/MySmartContract.sol
pragma solidity ^0.5.16;

contract MySmartContract {
function Hello() public view returns (string memory) {
return "Hello World";
}
function Greet(string memory str) public view returns (string memory) {
return str;
}
}
EOF

curl -OL https://binaries.soliditylang.org/linux-amd64/solc-linux-amd64-v0.5.16+commit.9c3226ce
chmod +x solc-linux-amd64-v0.5.16+commit.9c3226ce
./solc-linux-amd64-v0.5.16+commit.9c3226ce --optimize --abi ./contracts/MySmartContract.sol -o build

For me it prints this compiler output as expected:

./contracts/MySmartContract.sol:4:1: Warning: Function state mutability can be restricted to pure
function Hello() public view returns (string memory) {
^ (Relevant source part starts here and spans across multiple lines).
./contracts/MySmartContract.sol:7:1: Warning: Function state mutability can be restricted to pure
function Greet(string memory str) public view returns (string memory) {
^ (Relevant source part starts here and spans across multiple lines).
Refusing to overwrite existing file "build/MySmartContract.abi" (use --overwrite to force).

The pragma compiler issue is now gone.
This is what I got.

$ cd /tmp/
tmp$ 
tmp$ mkdir contracts/
tmp$ 
tmp$ 
tmp$ 
tmp$ cat << EOF > contracts/MySmartContract.sol
 
pragma solidity ^0.5.16;


contract MySmartContract {

function Hello() public view returns (string memory) {

return "Hello World";

}

function Greet(string memory str) public view returns (string memory) {

return str;

}

}

EOF
tmp$ 
tmp$ 
tmp$ 
tmp$ curl -OL https://binaries.soliditylang.org/linux-amd64/solc-linux-amd64-v0.5.16+commit.9c3226ce
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 7820k  100 7820k    0     0  10.4M      0 --:--:-- --:--:-- --:--:-- 10.4M
tmp$ 
tmp$ chmod +x solc-linux-amd64-v0.5.16+commit.9c3226ce
tmp$ 
tmp$ ./solc-linux-amd64-v0.5.16+commit.9c3226ce --optimize --abi ./contracts/MySmartContract.sol -o build
./contracts/MySmartContract.sol:8:1: Warning: Function state mutability can be restricted to pure
function Hello() public view returns (string memory) {
^ (Relevant source part starts here and spans across multiple lines).
./contracts/MySmartContract.sol:14:1: Warning: Function state mutability can be restricted to pure
function Greet(string memory str) public view returns (string memory) {
^ (Relevant source part starts here and spans across multiple lines).
Refusing to overwrite existing file "build/MySmartContract.abi" (use --overwrite to force).
tmp$ 

Great. Does it still work when you use your original snippet instead of the one I copied from your post?

No it does not. I think I found the issue. When I hover over

pragma solidity ^0.5.16;

in Visual Studio a balloon come up which says.
Source file requires different compiler version (current compiler is 0.8.6+commit.11564f7e.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version
Which contradicts

$ solc --version
solc, the solidity compiler commandline interface
Version: 0.5.16+commit.9c3226ce.Linux.g++ 

When I do

find / 0.8.6+commit.11564f7e.Emscripten.clang

Nothing is found.

Oh, so you’re using vscode-solidity extension and only seeing this problem inside VS? This extension does not use the solc you have installed locally. It downloads the compiler on its own (and it uses the emscripten builds rather than the native ones). Looks like it selects the latest version of the compiler by default and does not detect it based on the pragma. If you want 0.5.16, you have to configure it to use that version yourself. See Using a different version of the solidity compiler.

I installed the compiler via

sudo snap install solc

When I use the standard Ubuntu terminal to compile I get the same error:

$ solc --optimize --abi ./contracts/MySmartContract.sol -o build
./contracts/MySmartContract.sol:1:1: Warning: Source file does not specify required compiler version! Consider adding "pragma solidity ^0.5.16;"

Can you paste your code again, this time inside a ``` block? The one you pasted originally was malformed so I still don’t know if we’re even using the same code to test this.

pragma solidity ^0.5.16;


contract MySmartContract {
    function Hello() public view returns (string memory) {
        return "Hello World";
    }

    function Greet(string memory str) public view returns (string memory) {
        return str;
    }
}

This compiles fine for me locally. There’s something very weird going on. Are you 100% sure that this is the exact code you are compiling? Maybe you could post the AST the compiler sees. If it’s somehow compiling a different source file, this will make it apparent:

solc ./contracts/MySmartContract.sol --ast

I wonder if it might be because of snap. Since your solc is running inside it and you’re giving it a relative path, it might actually be compiling an entirely different file that’s located somewhere in its working directory.

By the way, I’d recommend not using snap to install the compiler anyway. From what I can see at snapcraft.io/solc it marks 0.5.16 as “stable” even though it’s ancient. There’s 0.6.8 “beta” but that’s quite old too. The snap page has not been updated in over a year. We’re at 0.8.6 now and that’s what I’d recommend using unless you have an older contract that you cannot upgrade. You can get up to date compiler binaries from solc-bin. It does not make all that much sense to use something like snap for it since the compiler is a single binary that you can just download and run.

Uninstalling the snap version did it.
Thanks for your help!!!

1 Like