简体   繁体   English

如何从shell脚本调用c函数?

[英]How to call c function from shell script?

请提及从 shell 脚本调用 c 函数的命令或示例代码。

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.不幸的是,您需要将 C 代码编译成一个可执行文件,如果您需要解析参数,可能需要使用命令行界面。 From there you can call your executable in the shell script.从那里你可以在 shell 脚本中调用你的可执行文件。

Python ctypes can be used to load native libraries and call functions that follow standard calling conventions. Python ctypes可用于加载本机库并调用遵循标准调用约定的函数。 So if Python is available, one can do something like:因此,如果 Python 可用,您可以执行以下操作:

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

You can use Tiny C compiler in immediate run mode, and run any program like this:您可以在立即运行模式下使用 Tiny C 编译器,并像这样运行任何程序

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

Tcc is super fast and really tiny. Tcc 超级快,而且非常小。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM