How to use compiled library with toolchain

I compile cJSOn library with openwrt SDK now i wrote a c program which uses the cjson library. can someone tell me how to use this build library to compile c code.

I already to tried to link it with -I/path/to/<target>/usr/include and -L/path/to/<target>/usr/lib. here is my command

$ mips-openwrt-linux-musl-gcc -L/home/admin/openwrt/staging_dir/target-mips_24kc_musl/usr/lib -I/home/admin/openwrt/staging_dir/target-mips_24kc_musl/usr/include parse_json.c -o pj

I am getting error of

parse_json.c:(.text+0x2e0): undefined reference to `cJSON_IsNumber

You are missing the name of the library to link to, so -lcjson should be appended to your command line.

As an alternative, you can link statically against it by just adding the .c and .h file to your project and adding it to your compilation commands.

1 Like

Hello, Thank you for your answer. I am able to compile the c program now. But when I transfer that executable file to destination system. It is not able to run it. It shows error.

Most likely not properly cross-compiled (it's easier to package up your program, so it does fully use the toolchain, rather than assembling the cross-compiler call yourself), as that looks very much like a libc mismatch (program built against glibc, OpenWrt requires musl).

Program is compiled with command mips-openwrt-linux-musl-gcc. I think it is musl which is required by openwrt.

I fully agree with @slh, creating a Makefile for the package is simple and you end up with a proper package.