简体   繁体   中英

What is the relation between Lua and C?

I am on an internship in a software company in a few weeks to figure out if I should start an apprenticeship as a software programmer. I have some (very) basic experience with Lua through gaming, which I stated in the resume for the internship, and they told me that they have a project where I could build on that experience. So I decided to get some more knowledge and fiddle around with Lua - leading me to this question.

I know Lua is a scripting language, and that it is often embedded in C programs. From what I understand Lua itself is written in very basic C and also has its own interpreter if used stand-alone. If that is the case, is it correct that:

No matter if I use Lua stand-alone or embedded in a C-Programming environment, underlying it will always use C-Code?

And since it is a scripting language, what I do with commands in Lua is basically accessing and commanding underlying C commands, as in telling C what to do?

No matter if I use Lua stand-alone or embedded in a C-Programming environment, underlying it will always use C-Code?

C is ordinarily a compiled language, so at runtime the C source code for Lua is not relevant any longer. But the machine code derived from that C source code will always be involved in executing your Lua code, so in that sense yes.

That does not necessarily mean that you need to know anything about C to write Lua code, however.

And since it is a scripting language, what I do with commands in Lua is basically accessing and commanding underlying C commands, as in telling C what to do?

Calling Lua a "scripting language" has more to do with the form of Lua code and the inspiration for its creation than it does with how Lua works . Notwithstanding any standalone interpreters, it is designed to be embedded in programs, for use in scripting behaviors of those programs . Certainly all of that is implemented in C, but as a Lua programmer, you should not think of it as Lua wrapping corresponding C functions.

Indeed, as a Lua programmer, you probably don't need to think about it at all. If you do think about it, however, then a better model is that a Lua-based scripting environment wraps program data and behaviors in a program-specific way.

Lua is an interpreted language or as the wiki informally puts it a "software processed" language. Lua is compiled into byte code (not to be confused with machine code.) and executed by a software interpreter at runtime. C is a compiled language that is compiled into machine code from source code and executed by hardware. If you want to get technical, C source code is not C anymore when it's compiled.

I use Lua obsessively and almost go out of my way just to use it. I try to implement it in all my projects, even projects that are already using an interpreted language. You can definitely get far on Lua alone but I strongly suggest you learn some C so that you can extend functionality. At the very least learn how to make DLLs that can be loaded as Lua modules. Alternatively learning C will be handy if you use LuaJIT since you can call C exported library functions directly with FFI. My final advice is you should read about the disadvantages of interpreted languages and Lua. You certainly wouldn't want to use it if your software needs to be highly secure or stable enough to autopilot an airplane, at which point an education in modern C++ is required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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