简体   繁体   English

GCC与.o文件链接

[英]GCC Linking with .o file

I'm trying to compile my code but it isn't working. 我正在尝试编译我的代码,但是它不起作用。 I got this to compile at school but I can't compile this on my home computer. 我可以在学校编译此文件,但无法在家用计算机上编译此文件。 I can't seem to figure out why. 我似乎不知道为什么。 I need this error fixed in order to continue my assignment. 我需要解决此错误才能继续我的作业。 Also, this list.o file is the profs file. 同样, 此list.o文件是profs文件。 I have to use this file. 我必须使用这个文件。

Solutions I've tried kind of? 我尝试过的解决方案有哪些? I've updated gcc to gcc-7. 我已经将gcc更新为gcc-7。 I've located libc.a 我已经找到libc.a

/usr/lib/x86_64-linux-gnu/libc.a
/usr/share/doc/libklibc/README.klibc.arch

Edit: I have tried compiling without -fPIC 编辑:我曾尝试不使用-fPIC进行编译

gcc -m64 -pthread -Wall -std=c99 -o run main.o as2.o list.o
/usr/bin/ld: list.o: relocation R_X86_64_32S against undefined symbol...

This is from the terminal: 这是从终端:

gcc -m64 -pthread -Wall -std=c99 -fPIC  -o run main.o as2.o list.o
/usr/bin/ld: list.o: relocation R_X86_64_32S against undefined symbol `headlist' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

This is my make file: 这是我的make文件:

CC = gcc
CFLAGS = -m64 -pthread -Wall -std=c99 -fPIC 
PROG = run
OBJS = main.o as2.o list.o

run: $(OBJS)
    $(CC) $(CFLAGS) -o $(PROG) $(OBJS)

main.o: main.c list.h as2.h
     $(CC) $(CFLAGS) -c main.c

as2.o: as2.c as2.h list.h
    $(CC) $(CFLAGS) -c as2.c

clean:
    rm main.o as2.o run

This is my includes: 这是我的包括:

#include <netinet/in.h> 
#include <sys/socket.h> 
#include <netdb.h>  
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <pthread.h> 
#include "list.h"
#include "as2.h"

Any ideas/solutions? 有什么想法/解决方案吗?

EDIT: changed the spelling from LFLAGS to LDFLAGS to appease some comment. 编辑:将拼写从LFLAGS更改为LDFLAGS以安抚一些评论。

EDIT: moved the -c option to appease some comment. 编辑:移动了-c选项以安抚一些评论。

Note: all is normally the first target in a make file. 注意: all通常是make文件中的第一个目标。

Note: used := rather than = so the macros would only be evaluated once. 注意:使用了:=而不是=因此宏只会被评估一次。

Note: the list.o is already available in the current directory (per the OPs question) so is not compiled in the makefile. 注意: list.o在当前目录中已经可用(根据OP问题),因此不会在makefile中进行编译。 But is only linked with the other object files to produce the executable 但是仅与其他目标文件链接以生成可执行文件

your makefile seems to have a few oversights. 您的makefile似乎有一些疏忽。 Suggest: 建议:

CC := gcc
RM := rm -f

CFLAGS := -ggdb -m64 -pthread -Wall -Wextra -pedantic -std=c99
#LDFLAGS :=   <-- use the default value

PROG := run
OBJS := main.o as2.o list.o
HDRS := list.h as2.h



.PSEUDO: all clean

all: $(PROG)

$(PROG): $(OBJS)
    $(CC)  -ggdb -o $(PROG) $(OBJS) $(LDFLAGS)

main.o: main.c $(HDRS)
     $(CC) $(CFLAGS) -c main.c -o main.o -I.

as2.o: as2.c $(HDRS)
    $(CC) $(CFLAGS) -c as2.c -o as2.o -I.

clean:
    $(RM) main.o as2.o run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM