c - 架构 x86_64 的未定义符号:“_read_line”,引用自:_insert in inventory-82371b.o
问题描述
这是带有 -v 和禁止警告的回调。
(base) goobnoob:~/c_projects/parts_database>gcc -w -v -o inventory inventory.c
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
"/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.10.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name inventory.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 /Library/Developer/CommandLineTools/usr/bin/../lib/clang/7.0.2 -w -fdebug-compilation-dir /Users/goobnoob/c_projects/parts_database -ferror-limit 19 -fmessage-length 278 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/9d/665ynkj530z1n3999g4lzyvr0000gn/T/inventory-9d3105.o -x c inventory.c
clang -cc1 version 7.0.2 based upon LLVM 3.7.0svn default target x86_64-apple-darwin14.5.0
ignoring nonexistent directory "/usr/local/include"
#include "..." search starts here:
#include <...> search starts here:
/Library/Developer/CommandLineTools/usr/bin/../lib/clang/7.0.2/include
/Library/Developer/CommandLineTools/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -w -o inventory /var/folders/9d/665ynkj530z1n3999g4lzyvr0000gn/T/inventory-9d3105.o -lSystem /Library/Developer/CommandLineTools/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"_read_line", referenced from:
_insert in inventory-9d3105.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
在这里,它没有被禁止警告。但我不相信是他们造成了这个问题。
(base) goobnoob:~/c_projects/parts_database>gcc -o inventory inventory.c
inventory.c:93:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
inventory.c:178:14: warning: data argument not used by format string [-Wformat-extra-args]
scanf("&d", &number);
~~~~ ^
inventory.c:212:75: warning: format specifies type 'long long' but the argument has type 'int' [-Wformat]
printf("%7d %-25s%lld\n", inventory[i].number, inventory[i].name, inventory[i].on_hand);
~~~~ ^~~~~~~~~~~~~~~~~~~~
%d
3 warnings generated.
Undefined symbols for architecture x86_64:
"_read_line", referenced from:
_insert in inventory-82371b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是代码。它是一个用于教科书上非常简单的数据库的程序。
我真的相信我的问题在于编译。自从我使用 gcc 已经有一段时间了,不记得我用过什么了。因为我现在不在,所以我将无法看到编译命令。
我也有支持的 readline.h 和 readline.c 文件。如果你需要那些只是问。
我也在 OS X Yosemite 的 UNIX 上运行它。
/* Maintains a parts database (array version) */
#include <stdio.h>
#include "readline.h"
#define NAME_LEN 25
#define MAX_PARTS 100
int main();
int find_part(int number);
void insert();
void search();
void update();
void print();
struct part {
int number;
char name[NAME_LEN + 1]; /* accounting for space needed for \n break */
int on_hand;
} inventory[MAX_PARTS];
int num_parts = 0; /* number of parts currently stored */
int find_part (int number);
void insert(void);
void search(void);
void update(void);
void print(void);
/******************************************************************
* main : Prompts the user to enter an operation code, then calls *
* a function to perform the requested action. Repeats until*
* the user enters the command 'q'. Prints an error message *
* if the user enters an illegal code. *
******************************************************************/
int main(void){
char code;
for (;;){
printf("Enter operation code: ");
scanf(" %c", &code);
while(getchar() != '\n'); /* skips to end of line */
switch (code) {
case 'i' : insert();
break;
case 's' : search();
break;
case 'u' : update();
break;
case 'p' : print();
break;
case 'q' : return 0;
default : printf("Illegal Code!\n");
}
printf("\n");
}
}
/****************************************************************
* find_part : Looks up a part number in the inventory array. *
* Returns the array index if the part number is *
* found; otherwise, returns -1. *
****************************************************************/
int find_part (int number){
int i;
for (i = 0; i < num_parts; i++){
if (inventory[i].number == number){
return i;
}
return -1;
}
}
/*****************************************************************
* insert : Prompts the user for information about a new part and *
* then inserts the part into the database. Prints an *
* error message and returns prematurely if the part *
* already exists or the database is full. *
*****************************************************************/
void insert(void){
int part_number;
if (num_parts == MAX_PARTS) {
printf("Database is full; can't add more parts.\n");
return;
}
printf("Enter part number: ");
scanf("%d", &part_number);
if (find_part(part_number) >= 0){
printf("Part already exists");
return;
}
/*Now we input the new part */
inventory[num_parts].number = part_number;
printf("Enter Part Name : ");
read_line(inventory[num_parts].name, NAME_LEN); /* Getting Part Name of no longer length than NAME_LEN */
printf("Enter quantity on hand : ");
scanf("%d", &inventory[num_parts].on_hand);
num_parts++; /* Updating amount of Parts in Storage */
}
/******************************************************************
* search : Prompts the user to enter a part number, then looks up *
* the part in the database. If the part exists, prints *
* the name and quantity on hand; if not, prints an error *
* message. *
******************************************************************/
void search(void){
int i, number;
printf("Enter part number : ");
scanf("%d", &number);
i = find_part(number);
if (i >= 0){ /* Verifying the part number exists (aka. did not return -1) */
printf("Part name : %s\n", inventory[i].name);
printf("Quantity on hand : %d\n", inventory[i].on_hand);
}else{
printf("Part not found!\n");
}
}
/****************************************************************
* update : Prompts the user to enter a part number. Prints an *
* error message if the part doesn't exist; otherwise, *
* prompts the user to enter change in quantity on hand *
* and updates the data base. *
****************************************************************/
void update(void){
int i, number, change;
printf("Enter Part Number : ");
scanf("&d", &number);
i = find_part(number);
if (i >= 0){
printf("Enter change in quantity on hand : ");
scanf("%d", &change);
inventory[i].on_hand += change;
}else{
printf("Part not found!\n");
}
}
/*****************************************************************
* print : Prints a listing of all parts in the database, showing *
* the part number, part name, and quantity on hand. *
* Parts are printed in the order in which they were *
* entered into the database. *
*****************************************************************/
void print(void){
int i;
printf("Part Number Part Name Quantity on Hand");
for (i = 0; i < num_parts; i++){
printf("%7d %-25s%lld\n", inventory[i].number, inventory[i].name, inventory[i].on_hand);
}
}
解决方案
我也有支持的 readline.h 和 readline.c 文件。如果你需要那些只是问。
您似乎根本没有编译 readline.c。尝试:
gcc -w -v -o inventory inventory.c readline.c
推荐阅读
- git - 从子模块获取远程找不到分支
- python - 使用 GEKKO 的 CSTR 稳态参数估计
- javascript - 读取文件行时控制加载顺序
- javascript - 为什么将对象初始化包装在函数中?
- css - 使用循环创建仅 CSS 的顺序淡入 DIV
- javascript - 错误:函数在请求范围外崩溃 函数调用被中断。有大名单
- mongodb - NestJS + Mongoose + GraphQL:“填充”不起作用
- mysql - 将 MySQL 查询迁移到 Postgres
- python - 如果我发现错误(我不需要在其中做任何事情),我可以简单地“通过”except 块吗?什么是最佳实践?
- excel - 我的 VBA 代码花费的时间太长,有什么建议可以缩短时间吗?循环