Compile embedded lua in C - c

Hey everyone I found this code that embeds Lua in C and I cannot figure out how to get GCC to compile it. I have Lua installed, but how do I link the Lua libraries?
Here is the code I found:
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
/* lua interpreter */
lua_State* l;
int main () {
int dofile;
/* initialize lua */
l = lua_open();
/* load lua libraries */
luaL_openlibs(l);
/* run the hello.lua script */
dofile = luaL_dofile(l, "hello.lua");
if (dofile == 0) {
/* call foo */
lua_getglobal(l,"foo");
lua_call(l,0,0);
}
else {
printf("Error, unable to run hello.lua\n");
}
/* cleanup Lua */
lua_close(l);
return 0;
}
How do I get this to compile?
I am trying this command to compile
gcc -o embed_hello -L/users/etrosclair/Downloads/lua-5.1.4 -I/users/etrosclair/Downloads/lua-5.1.4 luaTest.c
Here is the error:
Undefined symbols for architecture x86_64:
"_luaL_newstate", referenced from:
_main in ccF0995Q.o
"_luaL_openlibs", referenced from:
_main in ccF0995Q.o
"_luaL_loadfile", referenced from:
_main in ccF0995Q.o
"_lua_pcall", referenced from:
_main in ccF0995Q.o
"_lua_getfield", referenced from:
_main in ccF0995Q.o
"_lua_call", referenced from:
_main in ccF0995Q.o
"_lua_close", referenced from:
_main in ccF0995Q.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
All the lua libraries and headers are in the lua-5.1.4 folder the .o files are also in there too.
Thanks
Thanks

Depends if you want it statically or dynamically compiled.
For static, add -llua (or lua5.1 or lua51; depending on your setup)

Related

I run the libimobiledevice getting started code, got err?

I use gcc to complile the code
#include <stdio.h>
#include <stdlib.h>
#include <libimobiledevice/lockdown.h>
#include <libimobiledevice/libimobiledevice.h>
#include <plist/plist.h>
int main()
{
static char *udid = NULL;
/* Device Handle */
idevice_t device = NULL;
/* Try to connect to first USB device */
if (idevice_new_with_options(&device, NULL, IDEVICE_LOOKUP_USBMUX) != IDEVICE_E_SUCCESS) {
printf("ERROR: No device found!\n");
return -1;
}
/* Retrieve the udid of the connected device */
if (idevice_get_udid(device, &udid) != IDEVICE_E_SUCCESS) {
printf("ERROR: Unable to get the device UDID.\n");
idevice_free(device);
return -1;
}
/* Outputs device identifier */
printf("Connected with UDID: %s\n", udid);
/* Cleanup */
idevice_free(device);
free(udid);
}
got
Undefined symbols for architecture x86_64:
"_idevice_free", referenced from:
_main in 1-af7b2e.o
"_idevice_get_udid", referenced from:
_main in 1-af7b2e.o
"_idevice_new_with_options", referenced from:
_main in 1-af7b2e.o
ld: symbol(s) not found for architecture x86_64
while I have tried gcc -lstdc++ hello.c -o hello it's still cann't be compiled.
You need to include the libimobiledevice library, try this:
gcc hello.c -o hello -limobiledevice-1.0

CMAKE: how to include and link Lua 5.1 lib?

I'm super new to CMake, and for now it all looks like black magic to me.
Example that I token is from here!
How to properly include Lua(5.1) lib and link it with CMake?
Sample works when compiled by hands as in
$gcc -W -Wall -g -o main main.c -I/usr/local/include -L/usr/local/lib/lua/5.1 -llua
#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int main(void) {
puts("lua interpretor:");
char buff[256];
int error;
lua_State *L = luaL_newstate(); /* opens Lua */
luaL_openlibs(L); /* opens the standard libraries */
while (fgets(buff, sizeof(buff), stdin) != NULL) {
error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1); /* pop error message from the stack */
}
}
lua_close(L);
return 0;
}
current CMakeList.txt looks like
cmake_minimum_required(VERSION 3.8)
project(test)
set(CMAKE_C_STANDARD 11)
set(SOURCE_FILES main.c)
include_directories(/usr/local/include)
add_executable(test ${SOURCE_FILES})
and err.log
Undefined symbols for architecture x86_64:
"_luaL_loadbufferx", referenced from:
_main in main.c.o
"_luaL_newstate", referenced from:
_main in main.c.o
"_luaL_openlibs", referenced from:
_main in main.c.o
"_lua_close", referenced from:
_main in main.c.o
"_lua_pcallk", referenced from:
_main in main.c.o
"_lua_settop", referenced from:
_main in main.c.o
"_lua_tolstring", referenced from:
_main in main.c.o
You haven't linked the Lua library to the executable. This is done using target_link_libraries(). You'll need to locate or specify the location of the library first though. Luckily for you, Lua ships with a cmake script for finding Lua.
project(test)
find_package(Lua51 REQUIRED)
include_directories(/usr/local/include ${LUA_INCLUDE_DIR})
set(CMAKE_C_STANDARD 11)
set(SOURCE_FILES main.c)
add_executable(test ${SOURCE_FILES})
target_link_libraries(test ${LUA_LIBRARIES})

Error compiling allegro5 program using gcc on osx

I am trying to compile a simple allegro5 program on Mac OSX 10.12 but am getting an undefined symbols error. Here is the command I ran in the terminal
gcc main.c -o hello -I/usr/local/include/ -L/usr/local/lib -lallegro_main
And here is my code.
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
if(!al_init())
{
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display)
{
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
Here is the error I get
Undefined symbols for architecture x86_64:
"_al_clear_to_color", referenced from:
__al_mangled_main in main-b86b99.o
"_al_create_display", referenced from:
__al_mangled_main in main-b86b99.o
"_al_destroy_display", referenced from:
__al_mangled_main in main-b86b99.o
"_al_flip_display", referenced from:
__al_mangled_main in main-b86b99.o
"_al_install_system", referenced from:
__al_mangled_main in main-b86b99.o
"_al_map_rgb", referenced from:
__al_mangled_main in main-b86b99.o
"_al_rest", referenced from:
__al_mangled_main in main-b86b99.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Is it possible that I did not install allegro correctly? I installed it using homebrew according to the allegro wiki instructions. https://wiki.allegro.cc/index.php?title=Getting_Started#Mac_OS
Those are linker errors. You need to link to lallegro.

Compile Cilk plus in GCC5.2.0

Does anybody know how to compile the following code with Cilk plus in gcc5.2.0 correctly? With gcc -fcilkplus * or g++, I always get errors.
#include <cilk/cilk.h>
#include <assert.h>
int fib(int n) {
if (n < 2)
return n;
int a = cilk_spawn fib(n-1);
int b = fib(n-2);
cilk_sync;
return a + b;
}
int main() {
int result = fib(30);
assert(result == 832040);
return 0;
}
result:
Undefined symbols for architecture x86_64:
"___cilkrts_enter_frame_1", referenced from:
fib(int) in ccY1qrGL.o
"___cilkrts_enter_frame_fast_1", referenced from:
__cilk_spn_0 in ccY1qrGL.o
"___cilkrts_leave_frame", referenced from:
fib(int) in ccY1qrGL.o
__cilk_spn_0 in ccY1qrGL.o
"___cilkrts_rethrow", referenced from:
fib(int) in ccY1qrGL.o
"___cilkrts_save_fp_ctrl_state", referenced from:
fib(int) in ccY1qrGL.o
"___cilkrts_sync", referenced from:
fib(int) in ccY1qrGL.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
There are very few topics about this online. thanks
GCC is configured to add -lcilkrts by default to its linker command when -fcilkplus is given by users, but this default behavior can be overridden if there is any platform-specific configuration. I think that is happening on OS X, but it needs to be fixed in my opinion. Anyway, it seems that there is no short-term solution other than adding -lcilkrts as suggested above.

compiling a program that includes libmodbus in C

I am a newbie in C... I wrote a very simple modbus1.c that includes libmodbus (whose source I downloaded, unzipped, untarred, ./configure'd, make'd and make install'd successfully).
When I try to make modbus1.c I get this:
cc -Wall -g modbus1.c -o modbus1
Undefined symbols for architecture x86_64:
"_modbus_close", referenced from:
_main in modbus1-6cd135.o
"_modbus_connect", referenced from:
_main in modbus1-6cd135.o
"_modbus_free", referenced from:
_main in modbus1-6cd135.o
"_modbus_new_tcp_pi", referenced from:
_main in modbus1-6cd135.o
"_modbus_read_bits", referenced from:
_main in modbus1-6cd135.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [modbus1] Error 1
I am running OSX snow leopard and have successfully used make to compile small programs before (tutorial level programs...) Here is the modbus1.c I am trying to compile:
#include <stdio.h>
#include <stdlib.h>
#include <modbus.h>
int main(int argc, char *argv[]){
modbus_t *plc_client;
plc_client = modbus_new_tcp_pi("192.168.1.230","502");
if (plc_client == NULL) {
fprintf(stderr, "Unable to allocate libmodbus context\n");
return -1;
}
if (modbus_connect(plc_client) == -1) {
fprintf(stderr, "Connection failed: \n");
modbus_free(plc_client);
return -1;
}
else if(modbus_connect(plc_client) == 0) {
printf("MODBUS CONNECTION SUCCESSFUL\n");
}
uint8_t* catcher = malloc(sizeof(uint8_t));
if(modbus_read_bits(plc_client, 2000, 1, catcher)>0){
printf("READ SUCCESSFUL");
}
else{
printf("READ FAILED");
}
free(catcher);
modbus_close(plc_client);
modbus_free(plc_client);
return 0;
}
Any help will be greatly appreciated! Thanks!
-Niko
Try this
cc -Wall -g modbus1.c -o modbus1 -L/path/to/libmodbus -lmodbus
You should replace that /path/to/libmodbus with the actual path of directory that includes the libmodbus.dylib in your system.

Resources