gcc 无法使用make在c中编译多文件程序

3j86kqsm  于 2023-02-12  发布在  其他
关注(0)|答案(2)|浏览(140)

我试图编译以下多文件程序(取自书 * 编程:a Modern approach*,second edition By K. N. King)编译的代码,但是编译失败了,我收到了以下警告,但我不明白为什么:

clang: warning: readline.o: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: inventory2.o: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-Wall' [-Wunused-command-line-argument]

下面是生成文件:

############
# makefile #
############

CC = gcc
CFLAGS = -c -Wall

BIN=inventory2

$(BIN): $(BIN).o readline.o #readline.h
    $(CC) $(CFLAGS) readline.o $(BIN).o -o $(BIN)

$(BIN).o: $(BIN).c 
    $(CC) $(CFLAGS) $(BIN).c -o $(BIN).o

readline.o: readline.c readline.h
    $(CC) $(CFLAGS) readline.c -o readline.o

clean:
    $(RM) -rf $(BIN) *.dSYM
    $(RM) -rf readline.o
    $(RM) -rf $(BIN).o

run: all
    ./$(BIN)

下面是源文件(inventory2.creadline.creadline.h

/*********************************************************
 * From C PROGRAMMING: A MODERN APPROACH, Second Edition *
 * By K. N. King                                         *
 * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
 * All rights reserved.                                  *
 * This program may be freely distributed for class use, *
 * provided that this copyright notice is retained.      *
 *********************************************************/

/* inventory.c (Chapter 16, page 391) */
/* Maintains a parts database (array version) */

#include <stdio.h>
#include "readline.h"

#define NAME_LEN 25
#define MAX_PARTS 100

struct part {
  int number;
  char name[NAME_LEN+1];
  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.\n");
    return;
  }

  inventory[num_parts].number = part_number;
  printf("Enter part name: ");
  read_line(inventory[num_parts].name, NAME_LEN);
  printf("Enter quantity on hand: ");
  scanf("%d", &inventory[num_parts].on_hand);
  num_parts++;
}

/**********************************************************
 * 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) {
    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     *
 *         database.                                      *
 **********************************************************/
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\n");
  for (i = 0; i < num_parts; i++)
    printf("%7d       %-25s%11d\n", inventory[i].number,
           inventory[i].name, inventory[i].on_hand);
}

文件读取行.c

/*********************************************************
 * From C PROGRAMMING: A MODERN APPROACH, Second Edition *
 * By K. N. King                                         *
 * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
 * All rights reserved.                                  *
 * This program may be freely distributed for class use, *
 * provided that this copyright notice is retained.      *
 *********************************************************/

/* readline.c (Chapter 16, page 395) */

#include <ctype.h>
#include <stdio.h>
#include "readline.h"

int read_line(char str[], int n)
{
  int ch, i = 0;

  while (isspace(ch = getchar()))
    ;
  while (ch != '\n' && ch != EOF) {
    if (i < n)
      str[i++] = ch;
    ch = getchar();
  }
  str[i] = '\0';
  return i;
}

读取行h

/*********************************************************
 * From C PROGRAMMING: A MODERN APPROACH, Second Edition *
 * By K. N. King                                         *
 * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. *
 * All rights reserved.                                  *
 * This program may be freely distributed for class use, *
 * provided that this copyright notice is retained.      *
 *********************************************************/

/* readline.h (Chapter 16, page 395) */

#ifndef READLINE_H
#define READLINE_H

/**********************************************************
 * read_line: Skips leading white-space characters, then  *
 *            reads the remainder of the input line and   *
 *            stores it in str. Truncates the line if its *
 *            length exceeds n. Returns the number of     *
 *            characters stored.                          *
 **********************************************************/
int read_line(char str[], int n);

#endif
b5lpy0ml

b5lpy0ml1#

您可以使用隐式规则来简化Makefile:

CC = gcc
CFLAGS = -Wall

BIN = inventory2

.PHONY: run clean

$(BIN): $(BIN).o readline.o

readline.o: readline.h

clean:
    $(RM) $(BIN) *.o

run: $(BIN)
    -./$(BIN)
q7solyqu

q7solyqu2#

查看Make输出:

cc -c -Wall inventory2.c -o inventory2.o
cc -c -Wall readline.c -o readline.o
cc -c -Wall readline.o inventory2.o -o inventory2
cc: warning: readline.o: linker input file unused because linking not done
cc: warning: inventory2.o: linker input file unused because linking not done

您用来链接可执行文件的命令包含一个-c,这是因为您的标志中包含-c
此外,run方法依赖于不存在的目标。
从编译命令中删除-o,从链接命令中删除-c

############
# makefile #
############

CC=cc
CFLAGS=-Wall

BIN=inventory2

$(BIN): $(BIN).o readline.o #readline.h
    $(CC) $(CFLAGS) readline.o $(BIN).o -o $(BIN)

$(BIN).o: $(BIN).c 
    $(CC) $(CFLAGS) $(BIN).c -c

readline.o: readline.c readline.h
    $(CC) $(CFLAGS) readline.c -c

clean:
    $(RM) -rf $(BIN) *.dSYM
    $(RM) -rf readline.o
    $(RM) -rf $(BIN).o

run: $(BIN)
    ./$(BIN)

相关问题