Can';不要用我的C语言库

Can';不要用我的C语言库,c,libraries,c-libraries,C,Libraries,C Libraries,我使用的是OSX机器,我已经用C编写了一个库,我将在未来的程序中使用它 这是宣言:(FunzioniListe.h) 这是一个利用已创建的库的简单程序: #include <stdlib.h> #include <stdio.h> #include "FunzioniListe.h" int main() { struct node *first = NULL; for (int i = 0; i < 7; i++) { first

我使用的是OSX机器,我已经用C编写了一个库,我将在未来的程序中使用它

这是宣言:(FunzioniListe.h)

这是一个利用已创建的库的简单程序:

#include <stdlib.h>
#include <stdio.h>
#include "FunzioniListe.h"

int main() {
    struct node *first = NULL;
    for (int i = 0; i < 7; i++) {
        first = addElemToList(first, 30, i, i + 60);
    }
    printList(first);
}
#包括
#包括
#包括“FunzioniListe.h”
int main(){
结构节点*first=NULL;
对于(int i=0;i<7;i++){
first=加法器列表(first,30,i,i+60);
}
打印列表(第一);
}
我不明白问题是什么,两个文件都是正确的,但我的Mac电脑无法正确编译。 为什么会这样

深度调用错误:

Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name TestLibreria.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 253.9 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -fdebug-compilation-dir /Users/Matt/CProjects/Lab -ferror-limit 19 -fmessage-length 195 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/1c/6dpsqcrs69s9hgdnj0kkyh780000gn/T/TestLibreria-a8db8d.o -x c TestLibreria.c
clang -cc1 version 7.0.2 based upon LLVM 3.7.0svn default target x86_64-apple-darwin15.2.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.11.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -o a.out /var/folders/1c/6dpsqcrs69s9hgdnj0kkyh780000gn/T/TestLibreria-a8db8d.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "_addElemToList", referenced from:
      _main in TestLibreria-a8db8d.o
  "_printList", referenced from:
      _main in TestLibreria-a8db8d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Apple LLVM 7.0.2版(clang-700.1.81)
目标:x86_64-apple-darwin15.2.0
线程模型:posix
“/Applications/Xcode.app/Contents/Developer/toolschains/xcodefault.xctoolschain/usr/bin/clang”-cc1-triple x86_64-apple-macosx10.11.0-Wdeprecated objc isa用法-Werror=不推荐的objc isa用法-emit obj-mrelax all-disable free-disable llvm验证器-main file name TestLibreria.c-mrelocation model pic-pic level 2-mthread model posix-mdisable fp elim-masm verbose-munwind tables-target cpu core2-target linker version 253.9-v-dwarf列信息-resource dir/Applications/Xcode.app/Contents/Developer/toolschains/XcodeDefault.xctoolschain/usr/bin//lib/clang/7.0.2-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk-fdebug编译目录/Users/Matt/CProjects/Lab-ferror limit 19-fmessage长度195-堆栈保护器1-mstackrealign-fblocks-fobjc runtime=macosx-10.11.0-fencode扩展块签名-fmax type align=16-fdiagnostics show option-fcolor diagnostics-o/var/folders/1c/6DPSQCRS69HGDNJ0KYH78000GN/T/TestLibreria-a8db8d.o-x c TestLibreria.c
clang-cc1 7.0.2版基于LLVM 3.7.0svn默认目标x86_64-apple-darwin15.2.0
忽略不存在的目录“/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/local/include”
忽略不存在的目录“/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/Library/Frameworks”
#包括“…”搜索从这里开始:
#包括搜索从这里开始:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/./lib/clang/7.0.2/include
/Applications/Xcode.app/Contents/Developer/toolschains/xcodefault.xctoolschain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks(框架目录)
搜索列表结束。
“/Applications/Xcode.app/Contents/Developer/toolschains/xcodefault.xctoolschain/usr/bin/ld”-demangle-dynamic-arch x86_64-macosx_version_min 10.11.0-syslibroot/Applications/Xcode.app/Contents/Developer/Platforms/macosx.platform/Developer/SDKs/MacOSX10.11.sdk-o a.out/var/folders/1c/6dpsqcrs69s9hgdnj0kkyh78000gn/T/TestLibreria-a8db8d.o-lSystem/Applications/Xcode.app/Contents/Developer/toolschains/XcodeDefault.xctoolchain/usr/bin/./lib/clang/7.0.2/lib/darwin/libclang\u rt.osx.a
架构x86_64的未定义符号:
“附录列表”,引用自:
_TestLibreria-a8db8d.o中的主管道
“打印列表”,引用自:
_TestLibreria-a8db8d.o中的主管道
ld:找不到架构x86_64的符号
叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用)
创建C库的步骤
  • 创建库的接口


    • 带有库函数用户注释的函数原型
    • 库导出的类型和全局变量的定义
  • 创建库的实现


    • 库中每个函数的实现
  • 创建库对象文件


  • 当您拥有library.h接口及其library.c实现时,您只需运行以下命令

    gcc -o library.o -c library.c
    
  • 在另一个C程序中使用该库


    • 使用
      #在代码中包含“libraryname.h”
  • 我的代码中缺少的是步骤3

    现在我可以用命令编译使用我的库的C程序

    gcc cprogram.c library.o
    
    你不能把它们联系起来

    您可能只使用了
    gcc-std=c99 main.c
    (假设这是最后一个的名称)。那么使用的函数就不会被引用。在LinuxMint17.1Rebecca上运行之后,我得到了

    /tmp/ccOdy4br.o:在函数
    main'中:
    main.c:(.text+0x32):对
    addElemToList'的未定义引用 main.c:(.text+0x4c):对“打印列表”的未定义引用 collect2:错误:ld返回了1个退出状态

    所以您应该尝试使用链接标志

    gcc -std=c99 -c *.c
    
    然后编译它

    gcc -std=c99 *.o
    
    然后我可以运行它,并成功地得到一个分段错误


    注意:
    *
    是适用于任何应用程序的正则表达式,它在终端中工作,因为Mac基于Unix。

    请显示您的编译行。它们应该类似于
    gcc-o TestLibreria-ef7c77 TestLibreria-ef7c77.c FunzioniListe.o
    。顺便说一句,没有理由在
    FunzioniListe.h
    中包含
    stdio.h
    stdlib.h
    ,我已经编辑了该线程,如果您需要其他内容,请告诉我我猜您忘了在链接时间中包含库,如
    gcc-lmylib
    您没有显示编译行,但我在输出中看不到对
    FunzioniListe
    的任何引用,因此我假设您没有在编译中包含您的对象文件。添加它,就会找到您的函数。不要在结构指针和成员名称之间插入空格,例如
    list->next
    。这样做是非常罕见的,而且会使代码更难阅读。虽然功能正确,但步骤3D
    gcc cprogram.c library.o
    
    gcc -std=c99 -c *.c
    
    gcc -std=c99 *.o