Solaris 下无法使用信号量编译代码?
我编写了一些在 Linux 下可以正常编译的代码,但在 Solaris 上我遇到了一些编译错误。我使用 gcc test_compile.c -o tes -pthreads 进行编译。
#include <semaphore.h>
int main(){
sem_t semaphore;
sem_init(&semaphore, 0, 0);
return 0;
}
让我
itchy:~/edu/sysprog> gcc test_compile.c -o tes -pthreads
Undefined first referenced
symbol in file
sem_init /var/tmp//ccUiSK6A.o
ld: fatal: Symbol referencing errors. No output written to tes
不确定发生了什么事。我尝试用 sema_init 替换 sem_init 并编译(在网上的某个地方看到)。然而,这意味着我必须检查整个代码并用 sema 替换 sem。难道就没有更简单的解决方案吗?它的真正含义是什么?
I have written some code which compiles fine under linux but on Solaris I am given some compiling errors. I use gcc test_compile.c -o tes -pthreads
to compile.
#include <semaphore.h>
int main(){
sem_t semaphore;
sem_init(&semaphore, 0, 0);
return 0;
}
Gives me
itchy:~/edu/sysprog> gcc test_compile.c -o tes -pthreads
Undefined first referenced
symbol in file
sem_init /var/tmp//ccUiSK6A.o
ld: fatal: Symbol referencing errors. No output written to tes
I am not sure what's going on. I tryied replacing sem_init
with sema_init
and it compiles(saw that somewhere online). However that would mean that I must go through my whole code and replace sem with sema. Isn't there a simpler solution? And what does it really mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要链接实时扩展库
librt
:这记录在
sem_init(3RT)
的手册页中:You need to link with the realtime extensions library
librt
:This is documented in the man page for
sem_init(3RT)
:在这一切之前,我首先要确保您的链接正确。看起来编译得很好,但在链接步骤失败了。因此,首先确保您确实拥有 semaphore.h,并且它包含 sem_init(...)。如果您这样做,并且我的猜测是您这样做,请检查您的编译命令。如果这是您问题中的编译命令,请尝试将
-lpthread
添加到您的编译行以链接到 posix 线程库。因此,您应该仔细检查 sema 是否符合您的要求,因为它们是不同的库 -
sem
来自 POSIXpthreads
库,而sema
来自Solaristhread
库。 (另请参阅此内容)但是如果它们是代码兼容的,如果您正在寻找跨平台兼容的代码,您可能想要执行一些操作,例如创建简单的包装函数,然后有条件地包含它。您的包装器函数将非常简单,接受相同的类型并返回相同的类型,例如
,然后有条件地将其包含在类似
“注意 SOLARIS 不会被定义”之类的内容中。在Solaris 上编译时,您必须手动
#define SOLARIS
,或者在编译命令行/makefile 中定义它。至少我就是这么做的。
请注意,如果这不跨平台兼容,那么如果您只进行全局搜索和替换,则阅读和调试会容易得多。
Before all of this, I'd first make sure you're linking correctly. It seems like compiled fine, but failed at the linking step. So first make sure you actually have
semaphore.h
and that it includessem_init(...)
. If you do, and my guess is that you do, check your compile command. If that is your compile command in your question, try adding-lpthread
to your compile line to link in the posix thread library.So you should double check that sema does what you want, since they're different libraries --
sem
is from the POSIXpthreads
library andsema
is from the solaristhread
library. (See this also) But if they're code compatible, and if you're looking for cross-platform compatible code, you'd probably want to do something like create simple wrapper functions and then conditionally include it.Your wrapper functions would be really simple, accept the same types and return the same type, e.g.
And then conditionally include it with something like
Note that SOLARIS isn't going to be defined. You'll either have to manually
#define SOLARIS
when compiling on solaris, or define it in your compile command line / makefile.At least thats how I'd do it.
Note that if this is not to be cross-platform compatible, it's much easier to read and debug if you just do a global search and replace.