linking with two version of icu - gcc

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>

Related

Error while building a static Linux binary (with musl-libc) that includes LuaJIT

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.

Cannot Load C dynamic library with C Program compile with liblua.a (lua5.3)

I first download lua-5.3.5 , and put the source in my working directory and compile it with
make linux
so I got the liblua.a and lua binary file in ./lua-5.3.5/src.
And then I write a C Dynamic Library like this :
#include <stdio.h>
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
static int l_sin(lua_State *L)
{
double d = luaL_checknumber(L, 1);
lua_pushnumber(L, sin(d)); /* push result */
return 1; /* number of results */
}
static const struct luaL_Reg mylib[] = {
{"mysin", l_sin},
{NULL, NULL}
};
extern int luaopen_mylib(lua_State* L)
{
luaL_newlib(L, mylib);
return 1;
}
I compile with command :
gcc mylib.c -I ./lua-5.3.5/src -fPIC -shared -o mylib.so -Wall
And if I use the original lua binary, it can be loaded
user00:lua/ $ ./lua-5.3.5/src/lua
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
> require 'mylib'
table: 0xd13170
>
But If I write A C program linked with liblua.a, it cannot load the dynamic library.
#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(void){
char buff[256];
int error;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
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);
}
}
lua_close(L);
return 0;
}
Compile :
gcc test01.c -L ./lua-5.3.5/src/ -llua -lstdc++ -o test01 -lm -ldl -I ./lua-5.3.5/src
Run:
user00:lua/ $ ./test01
require 'mylib'
error loading module 'mylib' from file './mylib.so':
./mylib.so: undefined symbol: luaL_setfuncs
You need to export the Lua API functions from your executable. For this, link it with -Wl,-E as the Makefile from the Lua distribution does.

gcc error on trying to compile lua file

I am trying to create executable from lua file using following method:
I use bintocee utility (from: http://lua-users.org/wiki/BinToCee) to convert myfile.lua to code.c . I then use following main.c (from: Creating standalone Lua executables)
#include <stdlib.h>
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(int argc, char *argv[]) {
int i;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_newtable(L);
for (i = 0; i < argc; i++) {
lua_pushnumber(L, i);
lua_pushstring(L, argv[i]);
lua_rawset(L, -3);
}
lua_setglobal(L, "arg");
#include "code.c"
lua_close(L);
return 0;
}
Then I give command:
gcc main.c -o myfile.exe
However, I get following error:
/tmp/ccyIOC0O.o: In function `main':
main.c:(.text+0x21): undefined reference to `luaL_newstate'
main.c:(.text+0x2f): undefined reference to `luaL_openlibs'
main.c:(.text+0x41): undefined reference to `lua_createtable'
main.c:(.text+0x62): undefined reference to `lua_pushnumber'
main.c:(.text+0x82): undefined reference to `lua_pushstring'
main.c:(.text+0x92): undefined reference to `lua_rawset'
main.c:(.text+0xb7): undefined reference to `lua_setfield'
main.c:(.text+0xd5): undefined reference to `luaL_loadbuffer'
main.c:(.text+0xea): undefined reference to `lua_pcall'
main.c:(.text+0xf8): undefined reference to `lua_close'
collect2: error: ld returned 1 exit status
I am working on Linux Debian Stable (updated). Where is the problem and how can this be solved? Thanks for your help.
Since you installed liblua-5.1-dev, I assume you are on Debian or a derivative. There, you have to link with -llua5.1, like this:
gcc -O2 -Wall -I/usr/include/lua5.1 main.c -llua5.1

Receive error: undefined reference to static member while I have defined them in classname.cpp

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".

Can't Build a simple Cuda Program using Xcode !

I'm using Xcode 3.2 on Mac OS 10.6 to build a very simple HelloWorld program for CUDA
but it fails to build .. any ideas !!!
this is the code :
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUDA/CUDA.h>
__device__ char napis_device[14];
__global__ void helloWorldOnDevice(void){
napis_device[0]='H';
napis_device[1]='e';
napis_device[2]='l';
napis_device[3]='l';
napis_device[4]='o';
napis_device[5]=' ';
napis_device[6]='W';
napis_device[7]='o';
napis_device[8]='r';
napis_device[9]='l';
napis_device[10]='d';
napis_device[11]='\n';
}
int main (int argc, char * const argv[]) {
helloWorldOnDevice<<<1,1>>> ();
cudaThreadSynchronize();
char napis_host[14];
const char *symbol="napis device";
cudaMemcpyFromSymbol (napis_host, symbol, sizeof(char)*13, 0, cudaMemcpyDeviceToHost);
return 0;
}
The error appears at this line
helloWorldOnDevice<<<1,1>>> ();
Expected primary-expression before '<' token !!!!!!
You're compiling your program with gcc coming with Xcode. Should use nvcc compiler instead to compile CUDA code. Normally I would use a Makefile to tell that *.cu to be compiled by nvcc and *.cpp by gcc, then link produced objects to an executable.

Resources