hi.
i have written a program in c and also a makefile for it but i have a repetitive error:
/usr/bin/ld:4.txt: file format not recognized; treating as linker script
/usr/bin/ld:4.txt:1: syntax error

here is makefile code:

CC=gcc
test2 : test2.c 
        $(CC) test2.c 4.txt 5.txt 6.txt -I -lpthread -lrt -o test2

Dani AI

Generated

The linker error happens because you are handing your data files (4.txt, 5.txt, 6.txt) to gcc as if they were object files. ld then tries to interpret them and complains. Data files should not be listed as compilation/link inputs. Also note that -I only adds header include directories; it does not tell your program where to find data at runtime.

A simple pattern is: compile your program, keep the data files as dependencies so changes trigger a rebuild, and pass their paths to the program when you run it. For example:

CC := gcc
CFLAGS := -Wall -Wextra -O2
LDLIBS := -pthread -lrt

BIN := test2
SRC := test2.c
DATA := 4.txt 5.txt 6.txt

$(BIN): $(SRC) $(DATA)
    $(CC) $(CFLAGS) $< -o $@ $(LDLIBS)

.PHONY: run clean
run: $(BIN)
    ./$(BIN) 4.txt 5.txt 6.txt

clean:
    $(RM) $(BIN)

And in test2.c, open the files from argv rather than trying to compile them in:

int main(int argc, char **argv) {
    if (argc < 4) {
        fprintf(stderr, "usage: %s 4.txt 5.txt 6.txt\n", argv[0]);
        return 1;
    }
    /* fopen(argv[1]), fopen(argv[2]), fopen(argv[3]) ... */
}

Extra tips:

  • Keep libraries after objects in the link line (as above). If you are using POSIX threads, -pthread is usually preferable to -lpthread because it also sets needed compile/link flags.
  • If you truly want the text embedded in the binary, do not pass the .txt files directly; convert them first (e.g., with xxd -i to a header or objcopy to an object) and link the generated .o or include the generated .h. Otherwise, ’s point stands: they are runtime inputs, not build inputs.

Recommended Answers

All 3 Replies

What are 4.txt, 5.txt, and 6.txt?

names of files that test2.c will use information inside of them and are beside of test2.c
i tested -I for showing this but didn't work.

If they're data files, why are you trying to compile them?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.