“未定义的参考”在 Linux 中链接 C 代码时
我有一个代码库(完全用 C 编写),通常在 Windows 下将其编译为 .DLL
。
我想在 Linux 下编译它,这样我就可以分发它。我不在乎将其分发为 .a
、.so
还是一堆 .o
文件。
所有单独的 .c
文件均已成功编译。但是,当我尝试编译包含所有 .o
文件的测试可执行文件时,我收到一堆未定义的引用错误。
所有 .o
文件都作为完整路径出现在命令行上,并且我没有收到任何有关丢失文件的错误。
cc testctd.c -o testctd.out -lm -lc $LIBRARY-PATH/*.o
我还有对 _open
、_write
的未定义引用> 等
I have a code library (written entirely in C) that I typically compile under Windows, into a .DLL
.
I want to compile it under Linux so I can distribute it. I don't care if I distribute it as a .a
, a .so
, or a bunch of .o
files.
All of the individual .c
files compile successfully. But when I try to compile a test executable that includes all the .o
files, I get a bunch of undefined reference errors.
All of the .o
files are on the command line as full paths, and I don't get any errors about missing files.
cc testctd.c -o testctd.out -lm -lc $LIBRARY-PATH/*.o
I also have undefined references to _open
, _write
, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将 -l 选项放在了错误的位置
You have the -l options in the wrong place
您没有提供足够的信息来获得完整的答案,但我想我知道您的一个问题:函数
open
、read
、< code>write、close
等在 Windows 上其名称前面有下划线,但在 Linux(或任何其他 Unix 上)没有 )。当您编译.c
文件时,编译器应该警告您这一点 - 如果没有,请打开警告!无论如何,您必须删除所有这些下划线。我会推荐一个执行如下操作的头文件:然后在实际代码中仅使用无下划线名称。
此外,
-l
选项(例如-lm
)必须放置在所有目标文件之后。没有必要指定-lc
(并且它可能会导致问题,在这种情况下,这里无法讨论)。You haven't given enough information for a complete answer, but I think I know one of your problems: The functions
open
,read
,write
,close
, etc. have underscores in front of their names on Windows, but they do not on Linux (or any other Unix for that matter). The compiler should have warned you about that when you compiled the.c
files -- if it didn't, turn on warnings! Anyway, you're going to have to remove all those underscores. I would recommend a header file that does something like the following:and then use only the no-underscore names in your actual code.
Also,
-l
options (such as-lm
) must be placed after all object files. It is unnecessary to specify-lc
(and it may cause problems, under circumstances which are too arcane to go into here).