无法在 C 中使用我的库
Can't use my library in C
我正在使用 OS X 机器,我用 C 编写了一个库,我将在未来的程序中使用它。
这是声明:(FunzioniListe.h)
#ifndef FUNZIONILISTE_H
#define FUNZIONILISTE_H
#include <stdio.h>
#include <stdlib.h>
struct node {
int nodeValue;
char elem;
struct node *next;
};
struct node *addElemToList(struct node *list, int position, int value, char elemToAdd);
void deleteElem(struct node *list, int value);
int listLen(struct node *list);
void printList(struct node *list);
#endif // FUNZIONILISTE_H_INCLUDE
这是实现:(FunzioniListe.c)
#include <stdio.h>
#include <stdlib.h>
#include "FunzioniListe.h"
struct node *addElemToList(struct node *list, int position, int value, char elemToAdd) {
struct node *newNode;
newNode = malloc(sizeof(struct node));
if (newNode == NULL) {
printf("Error: malloc failed");
exit(EXIT_FAILURE);
}
newNode->elem = elemToAdd;
newNode->nodeValue = value;
if (position > listLen(list)) {
while (list->next != NULL) {
list = list->next;
}
list->next = newNode;
} else {
int counter = 0;
while (counter < position) {
list = list->next;
}
newNode->next = list->next;
list->next = newNode;
}
return newNode;
}
void deleteElem(struct node *list, int position) {
int counter = 0;
struct node *elemToDelete;
if (position > 0 && position < listLen(list)) {
while (counter < (position - 1)) {
list = list->next;
}
elemToDelete = list->next;
list->next = elemToDelete->next;
free(elemToDelete);
} else {
while (counter < (listLen(list) - 1)) {
list = list->next;
}
elemToDelete = list->next;
free(elemToDelete);
}
}
int listLen(struct node *list) {
int listLen = 0;
for (; list != NULL; list = list->next) {
listLen++;
}
return listLen;
}
void printList(struct node *list) {
for (; list != NULL; list = list->next) {
printf("Node #%d\nElem: %c\n\n", list->nodeValue, list->elem);
}
}
问题是,当我尝试在另一个项目中使用这个库时,我得到了这个输出:
Undefined symbols for architecture x86_64:
"_addElemToList", referenced from:
_main in TestLibreria-ef7c77.o
"_printList", referenced from:
_main in TestLibreria-ef7c77.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是一个利用已经创建的库的简单程序:
#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);
}
我不明白问题出在哪里,两个文件都是正确的,但是我的 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)
创建 C 库的步骤
创建库的接口
- 带有库函数用户评论的函数原型
- 您的库导出的类型和全局变量的定义
创建库的实现
- 库中每个函数的实现
创建库对象文件
当您拥有 library.h 接口及其 library.c 实现时,您所要做的就是运行 这个命令
gcc -o library.o -c library.c
在另一个 C 程序中使用库
- 在代码中使用
#include "libraryname.h"
我的代码中缺少的是第 3 步。
现在我可以使用命令编译使用我的库的 C 程序
gcc cprogram.c library.o
你不link他们。
您可能只使用 gcc -std=c99 main.c
(假设这是最后一个的名称)。然后使用的函数不会被引用。在运行之后 Linux Mint 17.1 Rebecca 我个人得到了
/tmp/ccOdy4br.o: In function main':
main.c:(.text+0x32): undefined reference to
addElemToList'
main.c:(.text+0x4c): undefined reference to `printList'
collect2: error: ld returned 1 exit status
所以你应该尝试 linking 标志
gcc -std=c99 -c *.c
然后编译
gcc -std=c99 *.o
然后我可以 运行 它并成功地得到一个 分段错误 。
注意:*
是 any 的正则表达式,它在终端中工作,因为 Mac 基于 Unix。
我正在使用 OS X 机器,我用 C 编写了一个库,我将在未来的程序中使用它。
这是声明:(FunzioniListe.h)
#ifndef FUNZIONILISTE_H
#define FUNZIONILISTE_H
#include <stdio.h>
#include <stdlib.h>
struct node {
int nodeValue;
char elem;
struct node *next;
};
struct node *addElemToList(struct node *list, int position, int value, char elemToAdd);
void deleteElem(struct node *list, int value);
int listLen(struct node *list);
void printList(struct node *list);
#endif // FUNZIONILISTE_H_INCLUDE
这是实现:(FunzioniListe.c)
#include <stdio.h>
#include <stdlib.h>
#include "FunzioniListe.h"
struct node *addElemToList(struct node *list, int position, int value, char elemToAdd) {
struct node *newNode;
newNode = malloc(sizeof(struct node));
if (newNode == NULL) {
printf("Error: malloc failed");
exit(EXIT_FAILURE);
}
newNode->elem = elemToAdd;
newNode->nodeValue = value;
if (position > listLen(list)) {
while (list->next != NULL) {
list = list->next;
}
list->next = newNode;
} else {
int counter = 0;
while (counter < position) {
list = list->next;
}
newNode->next = list->next;
list->next = newNode;
}
return newNode;
}
void deleteElem(struct node *list, int position) {
int counter = 0;
struct node *elemToDelete;
if (position > 0 && position < listLen(list)) {
while (counter < (position - 1)) {
list = list->next;
}
elemToDelete = list->next;
list->next = elemToDelete->next;
free(elemToDelete);
} else {
while (counter < (listLen(list) - 1)) {
list = list->next;
}
elemToDelete = list->next;
free(elemToDelete);
}
}
int listLen(struct node *list) {
int listLen = 0;
for (; list != NULL; list = list->next) {
listLen++;
}
return listLen;
}
void printList(struct node *list) {
for (; list != NULL; list = list->next) {
printf("Node #%d\nElem: %c\n\n", list->nodeValue, list->elem);
}
}
问题是,当我尝试在另一个项目中使用这个库时,我得到了这个输出:
Undefined symbols for architecture x86_64:
"_addElemToList", referenced from:
_main in TestLibreria-ef7c77.o
"_printList", referenced from:
_main in TestLibreria-ef7c77.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是一个利用已经创建的库的简单程序:
#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);
}
我不明白问题出在哪里,两个文件都是正确的,但是我的 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)
创建 C 库的步骤
创建库的接口
- 带有库函数用户评论的函数原型
- 您的库导出的类型和全局变量的定义
创建库的实现
- 库中每个函数的实现
创建库对象文件
当您拥有 library.h 接口及其 library.c 实现时,您所要做的就是运行 这个命令
gcc -o library.o -c library.c
在另一个 C 程序中使用库
- 在代码中使用
#include "libraryname.h"
- 在代码中使用
我的代码中缺少的是第 3 步。
现在我可以使用命令编译使用我的库的 C 程序
gcc cprogram.c library.o
你不link他们。
您可能只使用 gcc -std=c99 main.c
(假设这是最后一个的名称)。然后使用的函数不会被引用。在运行之后 Linux Mint 17.1 Rebecca 我个人得到了
/tmp/ccOdy4br.o: In function
main': main.c:(.text+0x32): undefined reference to
addElemToList' main.c:(.text+0x4c): undefined reference to `printList' collect2: error: ld returned 1 exit status
所以你应该尝试 linking 标志
gcc -std=c99 -c *.c
然后编译
gcc -std=c99 *.o
然后我可以 运行 它并成功地得到一个 分段错误 。
注意:*
是 any 的正则表达式,它在终端中工作,因为 Mac 基于 Unix。