Solaris 下无法使用信号量编译代码?

发布于 2024-12-14 05:19:20 字数 669 浏览 2 评论 0原文

我编写了一些在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李白 2024-12-21 05:19:20

您需要链接实时扩展库 librt

gcc test_compile.c -o tes -lrt -pthreads

这记录在 sem_init(3RT) 的手册页中:

SYNOPSIS
     cc [ flag... ] file... -lrt [ library... ]
     #include <semaphore.h>

     int sem_init(sem_t *sem, int pshared, unsigned int value);

You need to link with the realtime extensions library librt:

gcc test_compile.c -o tes -lrt -pthreads

This is documented in the man page for sem_init(3RT):

SYNOPSIS
     cc [ flag... ] file... -lrt [ library... ]
     #include <semaphore.h>

     int sem_init(sem_t *sem, int pshared, unsigned int value);
花间憩 2024-12-21 05:19:20

在这一切之前,我首先要确保您的链接正确。看起来编译得很好,但在链接步骤失败了。因此,首先确保您确实拥有 semaphore.h,并且它包含 sem_init(...)。如果您这样做,并且我的猜测是您这样做,请检查您的编译命令。如果这是您问题中的编译命令,请尝试将 -lpthread 添加到您的编译行以链接到 posix 线程库。


因此,您应该仔细检查 sema 是否符合您的要求,因为它们是不同的库 - sem 来自 POSIX pthreads 库,而 sema来自Solaris thread 库。 (另请参阅此内容)但是如果它们是代码兼容的,如果您正在寻找跨平台兼容的代码,您可能想要执行一些操作,例如创建简单的包装函数,然后有条件地包含它。

您的包装器函数将非常简单,接受相同的类型并返回相同的类型,例如

ret_type sem_init(argtype arg1, argtype arg2, argtype arg3)
{
    return sema_init(arg1, arg2, arg3)
}

,然后有条件地将其包含在类似

#ifdef SOLARIS
#include semaphore_wrappers.h
#endif

“注意 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 includes sem_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 POSIX pthreads library and sema is from the solaris thread 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.

ret_type sem_init(argtype arg1, argtype arg2, argtype arg3)
{
    return sema_init(arg1, arg2, arg3)
}

And then conditionally include it with something like

#ifdef SOLARIS
#include semaphore_wrappers.h
#endif

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文