mhw0 / libethc

Open-source Ethereum C library

Home Page:https://mhw0.github.io/libethc/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compile error

DerXanRam opened this issue · comments

commented

when i try to compile example "ERC20 balanceOf call" from the documentation it throughs this error

/usr/bin/ld: /home/biruk/Documents/eth/main.c:69: undefined reference to `eth_rlp_free'
collect2: error: ld returned 1 exit status

Build finished with error(s).

 *  The terminal process terminated with exit code: -1.  

I running ubuntu OS and all dependecies are installed correctly. I use Vscode as an IDE and the compiler(gcc) configuration "tasks.json" is :-

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        },
        {
            "type": "cppbuild",
            "label": "C/C++: gcc-11 build active file",
            "command": "/usr/bin/gcc-11",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Please help

commented

Hey @DerXanRam. It looks like it's an ld (linker) error, it can't find the eth_rlp_free symbol. Try to add-lethc flag to the args and see if it works.

Also, take a look at these:
https://stackoverflow.com/questions/52910102/vscode-c-task-json-include-path-and-libraries
https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

commented

Hey @DerXanRam. It looks like it's an ld (linker) error, it can't find the eth_rlp_free symbol. Try to add-lethc flag to the args and see if it works.

Also, take a look at these: https://stackoverflow.com/questions/52910102/vscode-c-task-json-include-path-and-libraries https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

It workes but throughs this warning
ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]

And thx very much for ur fast reply 🙏

commented

If it's c++, you could also add -Wno-write-strings (not sure) to args to suppress the error. Not safe, but should work :)

commented

And, where are you seeing this error? Could you tell me the name of the file and the line number?

commented

main.cpp file

#include <stdio.h>
#include <assert.h>
#include <ethc/account.h>
#include <ethc/ecdsa.h>
#include <ethc/rlp.h>
#include <ethc/keccak256.h>

#define ok(ethcop) assert(ethcop >= 0)

int main(void) {
  struct eth_account account;
  struct eth_rlp rlp0, rlp1;
  struct eth_ecdsa_signature sign;
  uint8_t privkey[] = {0xdf, 0x57, 0x08, 0x9f, 0xeb, 0xba, 0xcf, 0x7b,
                       0xa0, 0xbc, 0x22, 0x7d, 0xaf, 0xbf, 0xfa, 0x9f,
                       0xc0, 0x8a, 0x93, 0xfd, 0xc6, 0x8e, 0x1e, 0x42,
                       0x41, 0x1a, 0x14, 0xef, 0xcf, 0x23, 0x65, 0x6e};

  uint8_t nonce=0x00, zero=0x00, keccak[32], *rlp0bytes, *r, *s;
  char *gasprice = "0x04a817c800", *gaslimit = "0x5208", *value = "0x0de0b6b3a7640000";
  char *toaddr = "0x3535353535353535353535353535353535353535", *txn;
  uint16_t chainid = 0x7a69, v;
  size_t rlp0len, rlp1len, siglen=32;

  ok(eth_rlp_init(&rlp0, ETH_RLP_ENCODE));

  ok(eth_rlp_array(&rlp0));                  // [
    ok(eth_rlp_uint8(&rlp0, &nonce));        //   0x00,
    ok(eth_rlp_hex(&rlp0, &gasprice, NULL)); //   0x04a817c800, 
    ok(eth_rlp_hex(&rlp0, &gaslimit, NULL)); //   0x5208,
    ok(eth_rlp_address(&rlp0, &toaddr));     //   0x3535353535353535353535353535353535353535,
    ok(eth_rlp_hex(&rlp0, &value, NULL));    //   0x0de0b6b3a7640000,
    ok(eth_rlp_uint8(&rlp0, &zero));         //   0x,
    ok(eth_rlp_uint16(&rlp0, &chainid));     //   0x7a69,
    ok(eth_rlp_uint8(&rlp0, &zero));         //   0x,
    ok(eth_rlp_uint8(&rlp0, &zero));         //   0x,
  ok(eth_rlp_array_end(&rlp0));              // ]

  ok(eth_rlp_to_bytes(&rlp0bytes, &rlp0len, &rlp0));
  ok(eth_rlp_free(&rlp0));

  // compute the keccak hash of the encoded rlp elements
  ok(eth_keccak256(keccak, rlp0bytes, rlp0len));
  free(rlp0bytes);

  // sign the transaction
  ok(eth_ecdsa_sign(&sign, privkey, keccak));

  // calculate v
  v = sign.recid + chainid * 2 + 35;
  r = sign.r;
  s = sign.s;

  ok(eth_rlp_init(&rlp1, ETH_RLP_ENCODE));

  ok(eth_rlp_array(&rlp1));
    ok(eth_rlp_uint8(&rlp1, &nonce));
    ok(eth_rlp_hex(&rlp1, &gasprice, NULL));
    ok(eth_rlp_hex(&rlp1, &gaslimit, NULL));
    ok(eth_rlp_address(&rlp1, &toaddr));
    ok(eth_rlp_hex(&rlp1, &value, NULL));
    ok(eth_rlp_uint8(&rlp1, &zero));
    ok(eth_rlp_uint16(&rlp1, &v));
    ok(eth_rlp_bytes(&rlp1, &r, &siglen));
    ok(eth_rlp_bytes(&rlp1, &s, &siglen));
  ok(eth_rlp_array_end(&rlp1));

  ok(eth_rlp_to_hex(&txn, &rlp1));
  ok(eth_rlp_free(&rlp1));

  printf("Signed transaction: \n%s\n", txn);
  free(txn);
  return 0;
}
commented

The error log

[{
	"resource": "/home/biruk/Documents/eth/main.cpp",
	"owner": "cpptools",
	"severity": 4,
	"message": "ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]",
	"source": "gcc",
	"startLineNumber": 20,
	"startColumn": 20,
	"endLineNumber": 20,
	"endColumn": 20
}]
commented

Thanks for the detailed answer. I'll fix the warning and closing the issue.