I've cloned the LuaJIT git repo and built it with:
make STATIC_CC="musl-gcc" BUILDMODE="static"
Then, I compiled a simple Lua "hello world" script into a C header file:
luajit -b test.lua test.h
test.h:
#define luaJIT_BC_test_SIZE 52
static const unsigned char luaJIT_BC_test[] = {
27,76,74,2,10,45,2,0,3,0,2,0,4,54,0,0,0,39,2,1,0,66,0,2,1,75,0,1,0,20,72,101,
108,108,111,32,102,114,111,109,32,76,117,97,33,10,112,114,105,110,116,0
};
After that, I wrote a simple C wrapper by following the official example, test.c:
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "test.h"
int main(void) {
int error;
lua_State *L = lua_open();
luaL_openlibs(L);
error = luaL_loadbuffer(L, (const char *) luaJIT_BC_test, luaJIT_BC_test_SIZE, "test") || lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1);
}
lua_close(L);
return 0;
}
But when I try to build it, it crashes with an error:
$ musl-gcc -static -ILuaJIT/src -LLuaJIT/src -o test test.c -lluajit
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/libgcc_eh.a(unwind-dw2-fde-dip.o): in function `_Unwind_Find_FDE':
(.text+0x1953): undefined reference to `_dl_find_object'
collect2: error: ld returned 1 exit status
It's related to libgcc, so I tried building everything with musl-clang, but still got the same error. Can someone explain what I'm missing here?
Figured it out - I needed to build LuaJIT with TARGET_XCFLAGS=-DLUAJIT_NO_UNWIND like so:
make STATIC_CC="musl-gcc" BUILDMODE="static" TARGET_XCFLAGS=-DLUAJIT_NO_UNWIND
I guess this just disables C++ exceptions support, but I'm not sure what the real implications are. Seems to work fine, for now.
I tried to create simple program
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "/usr/local/icu/include/unicode/ustring.h"
UChar*
u_strdup(const UChar *in)
{
uint32_t len = u_strlen(in) + 1;
UChar *result = malloc(sizeof(UChar) * len);
if (!result)
return NULL;
u_memcpy(result, in, len);
return result;
}
int main()
{
return 0;
}
when i compile with
gcc test1.c -o tes1 `/usr/local/icu/bin/icu-config --ldflags`
/tmp/cc5h5bjr.o: In function `u_strdup':
test1.c:(.text+0x14): undefined reference to `u_strlen_50'
test1.c:(.text+0x50): undefined reference to `u_memcpy_50'
collect2: error: ld returned 1 exit status
as you can see, compiler still use the old version of icu
icu version:
50.1.2 (old, system)
60.2 (new)
What am I missing?
change your compile line to
gcc test1.c -o tes1 `/usr/local/icu/bin/icu-config --ldflags --cppflags`
and use #include <unicode/ustring.h>
I am playing around with using Semaphores, but I keep encountering Undefined Reference warnings, thus causing my code not to work. I pulled example code from a text, but was having issues with some of their syntax, so I went to POSIX's semaphore tutorial and changed things around to their syntax and as a result am now getting these reference errors.
I may simply be overlooking something, but I cannot find it.
Errors:
Producers_Consumers.c:52: warning: return type of ‘main’ is not ‘int’
/tmp/cceeOM6F.o: In function `producer':
Producers_Consumers.c:(.text+0x1e): undefined reference to `sem_init'
Producers_Consumers.c:(.text+0x3a): undefined reference to `sem_init'
Producers_Consumers.c:(.text+0x46): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x52): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x5e): undefined reference to `sem_post'
Producers_Consumers.c:(.text+0x6a): undefined reference to `sem_post'
/tmp/cceeOM6F.o: In function `consumer':
Producers_Consumers.c:(.text+0x7e): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x8a): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x96): undefined reference to `sem_post'
Producers_Consumers.c:(.text+0xa2): undefined reference to `sem_post'
collect2: ld returned 1 exit status
What I have (It may look a bit ugly due to the way I commented things out from my old method) I also know my adding method won't work, but I'll get to that when I fix my syntax issues:
#include <stdio.h>
#include <semaphore.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#define N 10 //Number of slots in buffer
typedef int semaphore; //Semaphores ae a special kind of int
sem_t mutex; //Controls access to critical region 1
sem_t empty; //Counts empty buffer slots N
sem_t full; //Counts full buffer slots 0
int count = 0; //What we're putting in
//int buffer[N];
void producer(void) {
sem_init(&mutex, 0, 1);
//sem_init(&empty, 0, N);
sem_init(&full, 0, 0);
while(1) {
sem_wait(&empty);
sem_wait(&mutex);
//printf("Empy: %d\n",empty);
//printf("Mutex: %d\n",mutex);
//printf("Both Downs Ran\n");
//buffer = buffer + 1;
sem_post(&mutex);
sem_post(&full);
//printf("Producer produced: %d\n",buffer);
}
}
void consumer(void) {
while(1) {
sem_wait(&full);
sem_wait(&mutex);
//item = buffer;
sem_post(&mutex);
sem_post(&empty);
//printf("Consumer consumed: %d/n",item);
}
}
void main() {
}
If you are on a linux system, you'll need to compile and link with the -pthread flag to link the pthreads library.
gcc -pthread Producers_Consumers.c
As Paul Griffiths has pointed out, you can also use -lrt, which is more portable, and links the POSIX Realtime Extensions library
gcc Producers_Consumers.c -lrt
Other notes specific to the code in the question:
int main(void) not void main()
typedef int semaphore is wrong, sem_t should be treated as an opaque type, you never use this typedef in your code anyway.
A problem I foresee is that your consumer code uses the semaphores before they are initialized in producer. You should initialize them in your main
Got same error in ubuntu qt.
After adding
LIBS += -lpthread -lrt
to project.pro file all compiled fine.
I am trying to define static members in a class and always received error undefined reference to static members.
I realize there are already many similar questions. But It seems that in those questions errors are raised because they did not define static members somewhere outside the class. I am sure that I have defined these static members.
The following is the problematic part of my code.
In foo.h, I defined a class named foo.
#include <random>
#include <vector>
class foo
{
public:
int random = dist(mt);
static std::random_device rd;
static std::mt19937 mt;
static std::uniform_int_distribution<int> dist;
static std::vector<int> ans(const int &);
};
In foo.cpp, there are definitions of static members.
#include<random>
#include<vector>
#include "foo.h"
std::random_device foo::rd;
std::uniform_int_distribution<int> foo::dist(0, 10);
std::mt19937 foo::mt(rd());
std::vector<int> foo::ans(const int & size) {
std::vector<int> bb;
for(int i=0; i < size; ++i)
bb.push_back(dist(mt));
return bb;
}
I used the class in another cpp file named test.cpp
#include <random>
#include <vector>
#include <iostream>
#include "foo.h"
int main()
{
foo a;
std::vector<int> c=a.ans(5);
for(auto it=c.begin(); it!=c.end(); ++it)
std::cout<<*it<<"\t";
std::cout<<"\n";
}
With g++ test.cpp -o a.out, I always received:
/tmp/ccUPnAxJ.o: In function `main':
test.cpp:(.text+0x37): undefined reference to `foo::ans(int const&)'
/tmp/ccUPnAxJ.o: In function `foo::foo()':
test.cpp:(.text._ZN3fooC2Ev[_ZN3fooC5Ev]+0xd): undefined reference to `foo::mt'
test.cpp:(.text._ZN3fooC2Ev[_ZN3fooC5Ev]+0x12): undefined reference to `foo::dist'
collect2: error: ld returned 1 exit status'
My g++ version is: g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2, and g++ is an alias to g++ -std=c++11.
You need:
g++ test.cpp foo.cpp -o a.out
Otherwise, how would g++ know about foo.cpp at all? It doesn't magically guess based on seeing #include "foo.h".
I wrote a hello world program to see how curses library works.
Here is my program:
/Users/snihalani/dev/daas at 10:10AM
➜ cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
int main(void)
{
int returnValue = 0;
while(1)
{
printf("I got %d\n", getch());
}
return 0;
}
I ran gcc main.c
I got
/Users/snihalani/dev/daas at 10:14AM
➜ gcc main.c
Undefined symbols for architecture x86_64:
"_stdscr", referenced from:
_main in ccEvUdhx.o
"_wgetch", referenced from:
_main in ccEvUdhx.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I don't what's going wrong. Can anyone please help?
Nevermind. I had to add -lcurses option while compiling.