使用gcc编译c代码的时候报错

今天编译erlang自带的ei_interface的例子时,报错如下
[liufan@liufan src]$ gcc -o extprg -I/usr/local/lib/erlang/lib/erl_interface-3.7.13/include/ -L/usr/local/lib/erlang/lib/erl_interface-3.7.13/lib complex.c erl_comm.c ei.c -lerl_interface -lei
/usr/local/lib/erlang/lib/erl_interface-3.7.13/lib/libei.a(ei_pthreads.o): In function `__erl_errno_place':
/home/liufan/workspace/sourceCode/otp_src_R16B01/lib/erl_interface/src/misc/ei_pthreads.c:194: undefined reference to `pthread_once'
/home/liufan/workspace/sourceCode/otp_src_R16B01/lib/erl_interface/src/misc/ei_pthreads.c:200: undefined reference to `pthread_getspecific'
/home/liufan/workspace/sourceCode/otp_src_R16B01/lib/erl_interface/src/misc/ei_pthreads.c:209: undefined reference to `pthread_setspecific'
/home/liufan/workspace/sourceCode/otp_src_R16B01/lib/erl_interface/src/misc/ei_pthreads.c:210: undefined reference to `pthread_getspecific'
/usr/local/lib/erlang/lib/erl_interface-3.7.13/lib/libei.a(ei_pthreads.o): In function `erl_errno_key_alloc':
/home/liufan/workspace/sourceCode/otp_src_R16B01/lib/erl_interface/src/misc/ei_pthreads.c:171: undefined reference to `pthread_key_create'
/usr/local/lib/erlang/lib/erl_interface-3.7.13/lib/libei.a(ei_pthreads.o): In function `ei_m_trylock':
/home/liufan/workspace/sourceCode/otp_src_R16B01/lib/erl_interface/src/misc/ei_pthreads.c:138: undefined reference to `pthread_mutex_trylock'
collect2: ld returned 1 exit status

经过再网上搜索得到需要再编译选项里加上-lpthread,经改正后,编译正常通过,效果如下:
[liufan@liufan src]$ gcc -o extprg -I/usr/local/lib/erlang/lib/erl_interface-3.7.13/include/ -L/usr/local/lib/erlang/lib/erl_interface-3.7.13/lib complex.c erl_comm.c ei.c -lerl_interface -lei -lpthread
[liufan@liufan src]$ 

在运行的时候按照文档里的步骤执行
Erlang R16B01 (erts-5.10.2) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V5.10.2  (abort with ^G)
1> c(complex2).
complex2.erl:43: Warning: variable 'Reason' is unused
{ok,complex2}
2> comp
compile     complex2    
2> complex2:start("extprg").
<0.40.0>
3> sh: line 0: exec: extprg: not found

却和文档写的不一样,经过自己修改后正确
Erlang R16B01 (erts-5.10.2) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V5.10.2  (abort with ^G)
1> complex2:start("./extprg").
<0.34.0>
2> 
2> complex2:foo(3).
4
3> complex2:bar(5).
10
4> complex2:st     
start/1  stop/0   
4> complex2:stop().
stop

猜你喜欢

转载自liumengfan.iteye.com/blog/1898157
今日推荐