Calling a Lua script from C: errors - c

I'm new at Lua and writing bindings in general. At the moment I'm just trying to compile the first example found here (with the functions updated to Lua 5.1).
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
/* the Lua interpreter */
lua_State* L;
int main ( int argc, char *argv[] )
{
/* initialize Lua */
L = luaL_newstate();
/* load various Lua libraries */
luaL_openlibs(L);
luaopen_table(L);
luaopen_io(L);
luaopen_string(L);
luaopen_math(L);
/* cleanup Lua */
lua_close(L);
return 0;
}
When I compile using gcc -o init init.c -Wall -I/usr/local/include -L/usr/local/lib -llua -lliblua I get the following error:
.../../i486-pc-linux-gnu/bin/ld: cannot find -lliblua
collect2: ld returned 1 exit status
The file liblua.a is in /usr/local/lib, but for some reason the compiler can't find it? What am I doing wrong?

The liblua.a file is included by the -llua parameter. Specifying -lliblua attempts to find a libliblua.a file that does not exist. So, simply remove -lliblua from your build command.

There's no -lliblua in 5.1.

Related

How to use WordNet in a C project?

I'm trying to link to WordNet library. I downloaded the header file (wn.h), libWN.a file and a bunch of .o files (libWN_a-search.o, etc) from WordNet. I put a main.c, wn.h and libWN.a files in a folder and implemented main.c:
#include <stdio.h>
#include "wn.h"
int main(int argc, char *argv[]) {
char rc, *s;
s = "cat";
rc = in_wn(s, ALL_POS);
printf("rc: %d\n", rc);
return 0;
}
I try to compile using cc main.c -L. but I get the linker error:
/usr/bin/ld: /tmp/ccvzqY4x.o: in function `main':
main.c:(.text+0x27): undefined reference to `in_wn'
collect2: error: ld returned 1 exit status
Documentation on WordNet lib can be found here. I'm just not sure what I'm doing wrong.
Including the wn.h header file is not enough. The .h file only contains the declarations, but the actual implemenation is contained in the library file libWN.a which you failed to link with.
For linking with that library you need to tell the compiler to do so by adding -lWN:
cc main.c -L. -lWN

How to fix 'undefined reference' when compiling in C?

I'm trying to compile a C program linking two previously created object files but keep getting an 'undefined reference' error.
I'm using Visual Code to write the code and Ubuntu on Windows to compile using a makefile. The two C files, task5.c and reverse.c which have been made into object files both contain #include reverse.h statements which contains the prototypes for the functions in reverse.c.
task5.c
#include <stdio.h>
#include "ctap.h"
#include "reverse.h"
TESTS {
const char *a = "Family";
char *b = reverse(a);
//test 1
ok(string_length(a) == string_length(b), "Strings are the same size");
//test 2
is("ylimaF", b, "Strings match");
}
reverse.c
#include <stdio.h>
#include <stdlib.h>
#include "reverse.h"
char *reverse(const char *str) {
//code
}
int string_length(const char *str) {
//code
}
reverse.h
char *reverse(const char *str);
int string_length(const char *str);
makefile
linked:
gcc -o linked task5.o reverse.o
task5.o
gcc -c -o task5.o task5.c
reverse.o
gcc -c -o reverse.o reverse.c
When I run the command make linked I expect it to be made and return nothing.
But when I run that command I get this error:
cc task5.o -o linked
task5.o: In function `ctap_tests':
task5.c:(.text+0x1abe): undefined reference to `reverse'
task5.c:(.text+0x1ace): undefined reference to `string_length'
task5.c:(.text+0x1adc): undefined reference to `string_length'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'linked' failed
make: *** [task5] Error 1
According to this GNU make documentation, the GNU make program will try to use GNUMakefile, makefile or Makefile.
The make program will not try makefile.mk, which means that e.g. make linked will use no makefile and only the default rules.
You can solve this by either renaming your makefile as Makefile (the most common) or makefile; Or by using the -f option to specify the makefile
$ make -f makefile.mk linked

Error in compiling before linking the object files using gcc

I have source files written in C programming using notepad++ and I am running them from command lines and later i need to link them inorder to generate the .exe file.
Here are the following commands I want to use while generating .exe file
gcc logc.c -o logc
gcc mainc.c -o mainc
gcc -o output logc.o mainc.o
But when i run the following command my compiler is returning with the following error status.
gcc logc.c -o logc
(x86)/mingw-w64/i686-8.1.0-win32-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x39): undefined reference to `WinMain#16'
when i run the following command to compile my mainc file
C:\Users\user\AppData\Local\Temp\ccskY3nf.o:mainc.c:(.text+0x31): undefined reference to `Log'
collect2.exe: error: ld returned 1 exit status
And here are my mainc.c and logc.c and logc.h files for your reference
logc.c file is here
#include <stdio.h>
#include "logc.h"
void InitLog()
{
Log("Initializing Log");
}
void Log(const char* message)
{
printf(" %s",message);
}
mainc.c file is here
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#include "logc.h"
int main()
{
int x = 5;
bool comparisonResult = x == 5;
if(comparisonResult == 1)
Log("Hello World");
return 0;
}
and logc.h file is here
#ifndef _LOG_H
#define _LOG_H
void InitLog();
void Log(const char* message);
#endif
How can i compile individual source files and then link them and generate an executable file.
Thanks in advance.
You don't create object files, for that you need the -c argument:
gcc logc.c -c
gcc mainc.c -c
gcc -o output logc.o mainc.o
By default gcc will generate an executable file, not an object file. So when you compile logc.c, it tries to make an executable but it can't find the main function so it fails. Similarly with main.c, it tries to make an executable but can't find Log
You need to add the -c option to create object files:
gcc logc.c -c -o logc.o
gcc mainc.c -c -o mainc.o

Libelf : Undefined Reference for functions

I am trying to use Libelf library to get some info on some elf files. But I keep getting these "undefined reference to [...]". I installed the libelf from the synaptic (tried to get from website too), and the lib seems to be instaled okay.
My code :
#include <err.h>
#include <fcntl.h>
#include <sysexits.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <libelf.h>
int main(int argc, char * argv[]) {
Elf *elf_file;
Elf_Kind elf_kind_file;
int file_descriptor;
if((argc!=2))
printf("Argumento em formato nao esperado\n");
file_descriptor = open(argv[1], O_RDONLY, 0);
elf_file = elf_begin(file_descriptor, ELF_C_READ, NULL);
elf_kind_file = elf_kind(elf_file);
elf_end(elf_file);
close(file_descriptor);
return 0;
}
Here is what I get from the terminal (Currently using Ubuntu 11.4):
gcc sample.c -o sample
/tmp/ccP7v2DT.o: In function `main':
sample.c:(.text+0x57): undefined reference to `elf_begin'
sample.c:(.text+0x67): undefined reference to `elf_kind'
sample.c:(.text+0x77): undefined reference to `elf_end'
collect2: ld returned 1 exit status
UPDATE
Solved all but one of my problems putting -lelf before my file during the compiling. The last one I could only solve updating my libelf to a new version (that wasn't available on the Synaptic Manager):
wget http://ftp.br.debian.org/debian/pool/main/e/elfutils/libelf-dev_0.153-1_i386.deb
wget http://ftp.br.debian.org/debian/pool/main/e/elfutils/libelf1_0.153-1_i386.deb
sudo dpkg -i libelf-dev_0.153-1_i386.deb libelf1_0.153-1_i386.deb
You probably need to link to the library. The standard way to do this is:
gcc sample.c -l elf -o sample
This searches the standard library directories for libelf.a, and includes the relevant object files in that archive into the build. Assuming the library has been installed correctly, libelf.a or a similarly-named file should exist.

Cuda mixed C project linking

I have a large project in C and i'm trying to integrate some Cuda kernels in it. I'm compiling my c-files with "gcc -c main.c" and my .cu files with "nvcc -c cuda_GMRES.cu" and then I try to link the 2 object files with nvcc: "nvcc -o main.o cuda_GMRES.o" and receive the following error:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function
_start':
(.text+0x20): undefined reference tomain'
collect2: ld returned 1 exit status
It's the first time I'm trying to combine cuda with C files and I might have done something wrong.Can someone help me please. I'm on a GPU Cluster with Rocks OS.
My main.c file:
#include <stdio.h>
#include <math.h>
#include "cuda_wrapper.h" //header containing wrapper function
//cuda_GMRES that calls the kernel cuda_dot
int main (int argc,char* argv[])
{
//content
//bla bla bla
//cuda Function call
cuda_GMRES(50);
return 0;
}
My cuda_wrapper.h file:
#ifndef Cuda_GMRES_cuda_wrapper_h
#define Cuda_GMRES_cuda_wrapper_h
//wrapper function declaration
void cuda_GMRES(double a);
#endif
My cuda_GMRES.cu file that contains the kernel calling function:
#include <stdio.h>
#include "cuda_wrapper.h"
#include "cuda_dot.cu"
//kernel declaration
__global__ void cuda_dot();
//kernel calling function
extern "C"
void cuda_GMRES(double a)
{
double b;
double *dev_a;
double *res;
cudaMemcpy(dev_a, &a, sizeof(double), cudaMemcpyHostToDevice );
cuda_dot<<< 1, 1 >>>(*dev_a, res );
cudaMemcpy(&b, res, sizeof(double), cudaMemcpyDeviceToHost );
}
My cuda_dot.cu file that contains the kernel:
__global__ void cuda_dot(double a, double *help)
{
*help=2*a;
}
Your linking command appears to contain a fatal error. Supposing you first compile two objects like this:
gcc -c main.c
nvcc -c cuda_GMRES.cu
you should have two object files main.o and cuda_GMRES.o. You then do this:
nvcc -o main.o cuda_GMRES.o
This command says "link a program file called main.o using cuda_GMRES.o", ie. overwrite main.o. It is for this reason that the linker is complaining about a missing main subroutine, you are not supplying one (and you are destroying the object file which contains one at the same time).
You want something like this:
nvcc -o executable main.o cuda_GMRES.o
where executable is the name of the final linked program, or
nvcc main.o cuda_GMRES.o
which will emit a default linked program called a.out

Resources