Lua 5.4 undefined reference with GCC - c

I'm trying to use gcc to dynamically link to the lua library but I keep getting undefined references to every single lua function, including luaL_newstate.
Here's the code I'm trying to compile
#include <stdlib.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
int main(void)
{
lua_State *state = luaL_newstate();
lua_close(state);
return 0;
}
And here's the command I'm using
cls && gcc test.c -Ilua-5.4.2_Win64_dllw6_lib\include -Llua-5.4.2_Win64_dllw6_lib -llua54
I also tried static linking with -l:liblua54.a, changing the order of the parameters, and moving the files into the mingw64 include and lib folders and using those instead, but nothing seems to change anything.

Related

ESP-IDF on Eclipse IDE error with external Library

I am using esp-idf v4.1.1 with different compilers, I have used Visual Studio Code and Eclipse IDE with Espressif tool installed.
My intention is that I want to use an external library that, at the moment, only has a function that does a SHA256 hash for which the openssl sha library (<openssl/sha.h>) is used.
The problem is that I include the library as a component to my project and I call it from the main but I get the following error when building the project.
(https://i.stack.imgur.com/3EECj.png)
If I try it in the Eclipse IDE I get more information about the error and I get "undefined reference to SHA256_INIT()" as for the rest of the functions.
See main.c, dual.c and dual.h code:
main.c:
#include <stdio.h>
#include "dual.h"
void app_main(void)
{
printf("Empezamos");
char * data = "hola";
char * e = generateHashSHA256(data);
printf("%s",e);
}
Dual.c:
#include <stdio.h>
#include "dual.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#include <openssl/sha.h>
char * generateHashSHA256(char *data){
SHA256_CTX ctx;
u_int8_t results[SHA256_DIGEST_LENGTH];
int n;
n = strlen(data);
SHA256_Init(&ctx);
SHA256_Update(&ctx, (u_int8_t *)data, n);
SHA256_Final(results, &ctx);
char *newString;
newString = malloc(sizeof(char)*SHA256_DIGEST_LENGTH*2);
memset(newString, 0, sizeof(char)*SHA256_DIGEST_LENGTH*2);
for(n=0;n<SHA256_DIGEST_LENGTH;n++)
{
printf(newString, "%s%02x", newString, results[n]);
}
return newString;
}
And Dual.h:
char * generateHashSHA256(char *data);
and CMake files:
CMake of component Dual:
idf_component_register(SRCS "dual.c"
INCLUDE_DIRS "include"
)
CMake of main folder:
idf_component_register(SRCS "main.c"
INCLUDE_DIRS ".")
CMake of project folder:
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(template-app)
I compiled the library from the terminal with "gcc -o name main.c -lssl -lcrypto" and it works correctly but when compiling it in an esp-idf project nothing...
Please HELP!
I have tried everything, I have included the openssl libraries in all the esp-idf directories, I have put the paths in the CMake... etc.

how to compile header files in c in visual studio code

I creating a program in c language and i using the Visual Studio Code for the first time, my functions in the header files don't function. This is my code in main:
#include <stdio.h>
#include "PilhaDinamica.h"
#include "PilhaEstatica.h"
int main()
{
Pilha *p = criaPilha();
return 0;
}
And this is my .h file:
#ifndef PILHADINAMICA_H_INCLUDED
#define PILHADINAMICA_H_INCLUDED
typedef struct Nodo{
char info;
struct Nodo*prox;
} nodo;
typedef struct {
nodo * Topo;
} Pilha;
Pilha * criaPilha();
int pilha_vazia(Pilha *p);
void push(Pilha *p, char times);
char pop(Pilha *p);
#endif
This is my file with the functions:
#include "PilhaDinamica.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Pilha *criaPilha()
{
Pilha *p = (Pilha*) malloc(sizeof(Pilha));
p->Topo = NULL;
return p;
}
And this is shown in my output: "...\AppData\Local\Temp\ccmjk1nS.o:main.c:(.text+0xf): undefined reference to `criaPilha'
collect2.exe: error: ld returned 1 exit status"
what can i do to make it compile correctly?
As a general rule of thumb, header files (*.h) contains declarations (type, variable and function declarations) and source files (*.c) the definitions of those declarations.
At the compilation step, only source files will be compiled (because the definitions are there). A program or library creation is a 2 (actually more, like preprocessing and more but for simplicity we keep it at 2) step process:
creating object files
e.g. gcc -c -o object_file_name.o source_file_name.c
link those object files into an executable or static/shared library
e.g. gcc -o program_or_library_name object_file_1.o object_file_2.o ...
So, in your case you have to call the compiler two times for your source files (with the -c flag) and once to link those created object files into an executable.
Note: If you're using a different compiler other than gcc, have a look at the documentation on how to create object files and link them together.

Where is random.h in msys2

I am using MSYS2 mingw 64 when compiling code that needs the header random.h I am trying to make that code work on both Linux and windows with the least amount of changes
#include <sys/random.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
srand(time(NULL));
return 0;
}
I ran this command pacman -S msys2-runtime-devel to download the random.h header file and it is located in sys official link
on linux, the file is included using #include <linux/random.c> but I don't know what to use on windows or if I have to do something completely different
When I comment the first line I get this warning
main.c:10:9: warning: implicit declaration of function 'srand' [-Wimplicit-function-declaration]
10 | srand(time(NULL));
| ^~~~~
As per the linked documentation,
srand is declared in #include <stdlib.h>.
rand is declared in #include <stdlib.h>.
Neither requires including random.h or linux/random.c.

storage size not constant when using offsetof and including termios.h with android ndk

I encounter a strange error when compiling this
#include <termios.h>
#include <stddef.h>
struct Test {
int a;
int b;
};
void test() {
static int test_array[(offsetof(struct Test,a)) > 0 ? 2 : 1];
}
with arm-linux-androideabi-gcc -c:
test.c:8:13: error: storage size of 'test_array' isn't constant
The strange thing about this is, when I remove the
#include <termios.h>
the whole thing compiles without error. Also, when I use the gcc installed on my ubuntu system, it does not complain independent of including termios.h.
Ayone any Idea why?
Background: I am trying to compile a ghc (glasgow haskell compiler) android crosscompiler, and hs2hsc fails on this situation.

Why can't my compiler find my library?

I'm trying to compile a simple C program using gcc:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lbryTest.h"
int main(int argc, char *argv[]) {
double x = 0.0;
x = lbryTest(2.0, 5.0);
printf("%f", x);
printf("Placeholder");
return 0;
}
lbryTest() is a function that does some simple math operations and returns a double. It is declared in lbryTest.h, and defined in liblbryTest.a.
When I compile the code using
gcc libraryTest.c -l lbryTest
I get the error:
cannot find -llbryTest.
How can I fix this?
Thanks to John Bollinger's comment, it finds the library. I added -L. to the command. This doesn't however answer why it was unable to find the library in the first place. The files are all in the same directory. How can I make it so the library will be found without explicitly adding the path? Or is that unconventional in C, and you should always explicitly add the path to all libraries linked?

Resources