2

I have code that #includes the files lua.h, lapi.h, lualib.h, and lauxlib.h from Lua's source. Now I have to actually compile this code.

My first thought is to include all of the .c files in the Lua's source code or just figure out which of those .c files I actually need, but is there a better or even right way to compile code that uses Lua 5.1's C API?

I should add that I am a complete beginning to compilation, and I know next to nothing. I do know what GCC is and how to run it from the command line, but that's about it.


To be more specific, I know that my compilation command using GCC will look something like this:

gcc code.c

Now, do I need to add every file in the Lua source as an argument like this?

gcc code.c lapi.c lauxlib.c lbaselib.c lcode.c …

or is there a better way to do this?

wsha
  • 874
  • 2
  • 9
  • 25
NetherGranite
  • 1,940
  • 1
  • 14
  • 42
  • 1
    The `gcc code.c` is wrong. In practice, even for a single translation unit `code.c` which does not use external library (e.g. a hello-world program), you should compile it with `gcc -Wall -Wextra -g code.c -o progbin` – Basile Starynkevitch Oct 06 '18 at 06:21
  • I have never seen any of those flags before except for `-o`, so as far as I understand, they aren't necessary. I am open to you telling why they are, however. As for `-o`, isn't it possible to compile and link in one step? I thought you only use `-o` if you want to compile code files to object files so you can link them manually. – NetherGranite Oct 06 '18 at 06:28
  • 1
    There are in practice absolutely necessary for software development. – Basile Starynkevitch Oct 06 '18 at 06:30

1 Answers1

3

A code that uses Lua API is just some C program which happens to use the Lua library. On my Linux/Debian system, that Lua library for developers is provided by the liblua5.3-dev Debian package. That package also has files for pkg-config

So you code your C program as usual. Probably, you'll use some build automation tool. On my Debian system, that could be make, ninja and many others. It is your choice and your responsibility to choose the right build automation tool.

Then you configure your build automation tool to pass relevant preprocessor flags to your C compiler, and to pass relevant linking flags at link time.

On my Linux system, I would choose to build with make, of course use the GCC compiler, and I would edit my Makefile (see this example) to contain things like:

CC= gcc
CFLAGS= -g $(shell pkg-config --cflags lua5.3) -Wall -Wextra
LDFLAGS+= $(shell pkg-config --libes lua5.3)

I do know what GCC is and how to run it from the command line,

You need to be sure to know how to practically use gcc to compile a program made of several translation units and using some external library. You also need to understand why build automation tools are so useful. Depending on your background, you might need to spend several weeks in reading more about that.

My recommendation: find some small free software program (e.g. on github) and study how it should be built (so learn enough to understand its build process). You'll learn a lot. Look into things like luakit

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Sorry, I'm a complete beginner when it comes to this. What exactly would I be building here? – NetherGranite Oct 06 '18 at 06:13
  • I don't even understand what you are asking. If you are a complete beginner to C, you'll better learn first how to *practically* use your C compiler (on the command line). What operating system are you on? – Basile Starynkevitch Oct 06 '18 at 06:15
  • I know how to use GCC on the command line. I know that the command will start with `gcc code.c`, but I don't know what to put beyond that. Should I just go through every file in the Lua source, doing something like `gcc code.c lapi.c lauxlib.c lbaselib.c …`, or is there a better way? – NetherGranite Oct 06 '18 at 06:18
  • There is a better way (you should use GNU `make`). And sorry, you don't really know how to use GCC on the commannd line. You should read [How to invoke GCC](https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html). Then you need to know how programs of several translation units are *practically* built – Basile Starynkevitch Oct 06 '18 at 06:19
  • Well, I'm looking to compile some code using the Lua C API for some Lua code I'm writing, not become a master of GCC. Can you tell me what exactly I'm missing? – NetherGranite Oct 06 '18 at 06:23
  • You miss the basics of build automation tool. And I am not asking you to become a master of GCC (I have 10 years of experience in contributing to the GCC compiler, and I don't feel being a "master" of it) – Basile Starynkevitch Oct 06 '18 at 06:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181383/discussion-between-basile-starynkevitch-and-nethergranite). – Basile Starynkevitch Oct 06 '18 at 06:30