Praveen
Praveen

Reputation: 12117

How to call c function from shell script?

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

Upvotes: 1

Views: 7590

Answers (5)

FrBrGeorge
FrBrGeorge

Reputation: 730

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.

Upvotes: 1

Karel Vlk
Karel Vlk

Reputation: 239

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.')"

Upvotes: 2

karel
karel

Reputation: 1347

Alternatively you can use this approach:

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

Upvotes: 0

Jokester
Jokester

Reputation: 5617

#!/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

Upvotes: -1

wim
wim

Reputation: 362627

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.

Upvotes: 4

Related Questions