1

Please mention command or sample code to call c function from shell script .

5 Answers 5

4

Unfortunately you would need to compile the C code into an executable file, possibly with a command line interface if you need to parse arguments. From there you can call your executable in the shell script.

Sign up to request clarification or add additional context in comments.

Comments

2

Python ctypes can be used to load native libraries and call functions that follow standard calling conventions. So if Python is available, one can do something like:

python -c "import ctypes;ctypes.CDLL('libc.so.6').puts('Hello from libC.')"

Comments

1

You can use Tiny C compiler in immediate run mode, and run any program like this:

A=100500
tcc -run - <<<"#include <stdio.h> int main() { printf(\"Hello, %d world.\n\", $A); }"

Tcc is super fast and really tiny.

Comments

0

Alternatively you can use this approach:

http://www.unix.com/shell-programming-scripting/51226-calling-c-function-froma-shell-script.html

Comments

-1
#!/usr/bin/bash
cat >temp.c <<BLAH_END
blah
function_you_want_to_call(arg_you_want_to_pass..);
BLAH_END
gcc temp.c -llibrary_you_want_to_link_to
./a.out

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.