使用 CMake 和 GCC 在 Mac 上构建静态库?
大家好,
我有一个静态库,稍后我将其链接到我的应用程序。我的开发环境是CMake、GCC(Linux、Mac)、MinGW(Windows)。
我可以在Linux和Windows上毫无问题地编译静态库。 (我什至可以在 Mac 上的应用程序中构建共享库)。
编辑:我将该库编译为共享库并且工作正常!!
我已按如下方式配置 CMakeFile 来构建静态库:
add_library(centi STATIC ${base_srcs} ${crv_srcs} ${node_srcs} ${trnk_srcs} ${defl_srcs} ${infl_srcs} ${track_srcs} ${callback_srcs} ${extract_srcs})
在链接阶段,它给出以下错误并构建“libcenti.a” “不知何故。
Linking C static library
lib/libcenti.a /usr/bin/ranlib: file:
lib/libcenti.a(crv_in_rgn_to_bnry_img.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_initialize_by_circle.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_initialize_flgs.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_nodal_interval_min_and_max.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_remove_all_nodes.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_reset_nodal_forces.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_set_center_coords.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_set_left_ptch_rgn_pixs.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_set_out_rgn_mean_and_var.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_set_para.c.o) has
no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_set_right_ptch_rgn_pixs.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_to_in_rgn_hist.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_to_out_rgn_pixs.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(trnk_initialize_by_circle.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(trnk_initialize_by_image_frame.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(trnk_stk_paint_nodes_and_pixs.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(trnk_stk_to_inner_defl_ordn.c.o)
has no symbols
但是当我将上面的库与我的应用程序链接时,它会给出“未定义的符号”错误:
Undefined symbols:
"_setActiveDrawingTrunk", referenced
from:
RzPluginAlgoCnty::initCallBacks()
in RzPluginAlgoCnty.cpp.o
RzPluginAlgoCnty::clearCallBacks()
in RzPluginAlgoCnty.cpp.o
_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_bg_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_bg_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_extract_contour_update_tracking in
libcenti.a(extract_contour_update_tracking.c.o)
_extract_contour_update_tracking in
libcenti.a(extract_contour_update_tracking.c.o)
"_updateCurveUICallBack", referenced
from:
RzPluginAlgoCnty::initCallBacks()
in RzPluginAlgoCnty.cpp.o
RzPluginAlgoCnty::initBulkCallBacks()
in RzPluginAlgoCnty.cpp.o
RzPluginAlgoCnty::clearCallBacks()
in RzPluginAlgoCnty.cpp.o
_bg_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_bg_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_crv_update_1time in libcenti.a(crv_update_ordn.c.o)
_crv_update_1time in libcenti.a(crv_update_ordn.c.o) ld:
symbol(s) not found
有什么提示吗?在 Mac 上构建静态库时是否应该添加任何特殊参数?
编辑: 我将该库编译为共享库,并且运行良好!!
Greetings all,
I have a static library which I later link with my application. My development environment is CMake, GCC (Linux, Mac), MinGW (Windows).
I can compile the static library without any problem on Linux and Windows. (I can even build shared libraries in my application on Mac).
EDIT: I compiled the library as a SHARED library and it worked fine!!
I have configured CMakeFile as follows to build the static library:
add_library(centi STATIC ${base_srcs} ${crv_srcs} ${node_srcs} ${trnk_srcs} ${defl_srcs} ${infl_srcs} ${track_srcs} ${callback_srcs} ${extract_srcs})
During linking phase, it gives following errors and build the "libcenti.a" somehow.
Linking C static library
lib/libcenti.a /usr/bin/ranlib: file:
lib/libcenti.a(crv_in_rgn_to_bnry_img.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_initialize_by_circle.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_initialize_flgs.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_nodal_interval_min_and_max.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_remove_all_nodes.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_reset_nodal_forces.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_set_center_coords.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_set_left_ptch_rgn_pixs.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_set_out_rgn_mean_and_var.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_set_para.c.o) has
no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_set_right_ptch_rgn_pixs.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_to_in_rgn_hist.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(crv_to_out_rgn_pixs.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(trnk_initialize_by_circle.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(trnk_initialize_by_image_frame.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(trnk_stk_paint_nodes_and_pixs.c.o)
has no symbols /usr/bin/ranlib: file:
lib/libcenti.a(trnk_stk_to_inner_defl_ordn.c.o)
has no symbols
But when I link above library with my application, it gives "Undefined symbols" errors:
Undefined symbols:
"_setActiveDrawingTrunk", referenced
from:
RzPluginAlgoCnty::initCallBacks()
in RzPluginAlgoCnty.cpp.o
RzPluginAlgoCnty::clearCallBacks()
in RzPluginAlgoCnty.cpp.o
_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_bg_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_bg_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_extract_contour_update_tracking in
libcenti.a(extract_contour_update_tracking.c.o)
_extract_contour_update_tracking in
libcenti.a(extract_contour_update_tracking.c.o)
"_updateCurveUICallBack", referenced
from:
RzPluginAlgoCnty::initCallBacks()
in RzPluginAlgoCnty.cpp.o
RzPluginAlgoCnty::initBulkCallBacks()
in RzPluginAlgoCnty.cpp.o
RzPluginAlgoCnty::clearCallBacks()
in RzPluginAlgoCnty.cpp.o
_bg_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_bg_trnk_trck_ordn in libcenti.a(trnk_trck_ordn.c.o)
_crv_update_1time in libcenti.a(crv_update_ordn.c.o)
_crv_update_1time in libcenti.a(crv_update_ordn.c.o) ld:
symbol(s) not found
Any tips ? Should I add any special parameters when building static libraries on Mac?
EDIT:
I compiled the library as a SHARED library and it worked fine!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,设置-no_warning_for_no_symbols可能还不够。 Mac 上的 ar 命令将为您运行 ranlib,这会导致显示“没有符号 /usr/bin/ranlib”消息。您可以使用以下 CMake 规则在 Mac 上停止此消息:
ARCHIVE_CREATE“Scr”设置将阻止 ar 为您运行 ranlib。 ARCHIVE_FINISH 设置会阻止 ranlib 抱怨缺少符号。
您需要使用两组规则来阻止“无符号”消息。
对于 CMake 新手来说,还有一件事:CMake 为 ar 和 ranlib 命令生成的命令被转储到 link.txt 文件中。如果您在生成存档时遇到问题,您可能需要查看 link.txt 以了解 CMake 正在为您做什么。
Unfortunately, setting -no_warning_for_no_symbols may not be enough. The ar command on the Mac will run ranlib for you, which causes the "has no symbols /usr/bin/ranlib" message to be displayed. You can stop this message on the Mac by using the following CMake rules:
The ARCHIVE_CREATE "Scr" setting will keep ar from running ranlib for you. The ARCHIVE_FINISH setting will gag ranlib from complaining about missing symbols.
You need to use both sets of rules to gag "no symbols" message.
One more thing for CMake newbies: The commands CMake generates for the ar and ranlib commands are dumped into a link.txt file. If you're having problems generating archives, you may want to look at link.txt to see what CMake is doing for you.
来自
ranlib
的警告表明这些目标文件不包含任何操作代码。您需要检查每个相应的源文件,以了解其包含的代码是否应在 Mac 上编译。可能代码已移植到其他地方,但条件编译无法正确识别 Mac。这不是一个“灌篮高手”的诊断;而是一个“灌篮”诊断。丢失的符号显然分为两组 - 与
trnk
相关的组和与crv
相关的组 - 并且文件同样分为两组(前缀为trnk
> 和crv
),但这些文件可能定义了不同的函数。因此,我会查看与空目标文件相对应的源文件,看看是否可以看到丢失的函数(可能忽略报告的丢失符号名称上的前导下划线)。如果这些函数是在这些文件中定义的,那么您必须查看来自ranlib
的投诉。有时,如果您在不方便的时候耗尽了磁盘空间,则可能会得到零大小的目标文件(因此,它们不包含任何符号)。在这种情况下,只需删除空的对象文件(并释放一些空间,但您必须已经这样做)并重新构建。
The warnings from
ranlib
indicate that those object files contained no operational code. You need to review each corresponding source file to see whether the code it contains should be compiled on Mac. It may be that the code was ported elsewhere but the conditional compilation doesn't recognize Mac correctly.It isn't a 'slam dunk' diagnosis; the missing symbols are clearly in two groups - those related to
trnk
and those related tocrv
- and the files are likewise in two groups (prefixedtrnk
andcrv
), but it could be that the files define different functions. So, I would go looking at the source files corresponding to the empty object files and see if you can see the missing functions (probably ignore the leading underscore on the reported missing symbol names). If those functions are defined in those files, then you have to look at the complaints fromranlib
.Occasionally, if you ran out of disk space at an inconvenient moment, you could have zero-size object files (which contain no symbols, therefore). In that case, simply remove the empty object files (and free up some space, but you must already have done that) and build again.
这里讨论了 MacOSX 上静态库的类似问题: http:// /lists.macosforge.org/pipermail/macports-tickets/2008-June/010680.html
尝试进行完全清理并运行非并行构建。
和 本指南建议先安装macports,然后“sudo port install autogen autoconf automake nano libtool libsdl patchutils subversion wget gmake”,然后export PATH="/usr/local/bin:/usr/local/sbin:$小路”
The similar problem with static libraries on MacOSX is discussed here: http://lists.macosforge.org/pipermail/macports-tickets/2008-June/010680.html
Try to do a full clean and run a not-parallel build.
And this guide recommends to install macports, then "sudo port install autogen autoconf automake nano libtool libsdl patchutils subversion wget gmake", and then export PATH="/usr/local/bin:/usr/local/sbin:$PATH"