gpt4 book ai didi

c++ - 使用 gcc 和 g++ 分别编译包含 C 和 C++ 文件的项目?

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:35 25 4
gpt4 key购买 nike

我有一个包含 C 和 C++ 文件的项目,我创建了一个 Makefile 来编译这个项目。

是否可以有一个 Makefile 来“分离”C 和 C++ 文件,C 文件将用 gcc 编译,C++ 文件用 g++ 编译并创建一个二进制文件?

这是我创建的 Makefile:

TECHAUDIO_ROOT = ../../../../../../..
TECHAUDIO_GENERIC = $(TECHAUDIO_ROOT)/Generic
TECHAUDIO_MALLOC_3DSL = $(TECHAUDIO_ROOT)/Tools/Malloc_3DSL
TECHAUDIO_SIGPROC_COMMON_SRC = ../../../../SignalProcessing/RefFT/Common/src
TECHAUDIO_SIGPROC_SRC = ../../../../SignalProcessing/RefFT/Float/src
TECHAUDIO_LIMOTOR_SRC = ../../../RefFT/Float/src
TECHAUDIO_LIMOTOR_COMMON_SRC = ../../../RefFT/Common/src/API

INCLUDES += -I$(TECHAUDIO_GENERIC)/Common/Include \
-I$(TECHAUDIO_GENERIC)/X86/Include \
-I$(TECHAUDIO_MALLOC_3DSL)/RefFT/Common/Include \
-I$(TECHAUDIO_SIGPROC_COMMON_SRC) \
-I$(TECHAUDIO_SIGPROC_SRC)/Math \
-I$(TECHAUDIO_SIGPROC_SRC)/Transform \
-I$(TECHAUDIO_SIGPROC_SRC)/Filter \
-I$(TECHAUDIO_SIGPROC_SRC)/Vector \
-I$(TECHAUDIO_SIGPROC_SRC)/SSTP \
-I$(TECHAUDIO_LIMOTOR_SRC)/API \
-I$(TECHAUDIO_LIMOTOR_COMMON_SRC) \
-I$(TECHAUDIO_LIMOTOR_SRC)

SRCS = $(TECHAUDIO_LIMOTOR_SRC)/LIMITOR_main_32f.c \
$(TECHAUDIO_LIMOTOR_SRC)/API/LIMITOR_API_32f.c \
$(TECHAUDIO_LIMOTOR_SRC)/API/LIMITOR_API_coordinate_32f.c

SRCSCPP = $(TECHAUDIO_LIMOTOR_SRC)/Test/LIMITOR_TEST_example_32f.cpp
# $(TECHAUDIO_LIMOTOR_SRC)/VST/LIMITOR_VST_audioEffect_32f.cpp \
# $(TECHAUDIO_LIMOTOR_SRC)/VST/LIMITOR_VST_main_32f.cpp

NAME = Bonjour

CC = gcc

CPP = g++

CFLAGS += $(INCLUDES)
CFLAGS += -D_X86_GCC_4_1_1_TARGET_
#CFLAGS +=-D_X86_VC8_TARGET_
RM = rm -f

OBJ= $(SRCS:.c=.o)

OBJCPP= $(SRCSCPP:.cpp=.o)

all: $(NAME)

$(NAME): $(OBJ)
$(CC) $(CFLAGS) $(OBJ) -o $(NAME) -lm
$(OBJCPP)
$(CPP) $(CFLAGS) $(OBJCPP) -o $(NAME) -lm

clean:
$(RM) $(OBJ) $(OBJCPP)

fclean: clean
$(RM) $(NAME)
re: fclean all

编辑

下面是 Makefile 的更新版本:

TECHAUDIO_ROOT = ../../../../../../..
TECHAUDIO_GENERIC = $(TECHAUDIO_ROOT)/Generic
TECHAUDIO_MALLOC_3DSL = $(TECHAUDIO_ROOT)/Tools/Malloc_3DSL
TECHAUDIO_SIGPROC_COMMON_SRC = ../../../../SignalProcessing/RefFT/Common/src
TECHAUDIO_SIGPROC_SRC = ../../../../SignalProcessing/RefFT/Float/src
TECHAUDIO_LIMOTOR_SRC = ../../../RefFT/Float/src
TECHAUDIO_LIMOTOR_COMMON_SRC = ../../../RefFT/Common/src/API

INCLUDES += -I$(TECHAUDIO_GENERIC)/Common/Include \
-I$(TECHAUDIO_GENERIC)/X86/Include \
-I$(TECHAUDIO_MALLOC_3DSL)/RefFT/Common/Include \
-I$(TECHAUDIO_SIGPROC_COMMON_SRC) \
-I$(TECHAUDIO_SIGPROC_SRC)/Math \
-I$(TECHAUDIO_SIGPROC_SRC)/Transform \
-I$(TECHAUDIO_SIGPROC_SRC)/Filter \
-I$(TECHAUDIO_SIGPROC_SRC)/Vector \
-I$(TECHAUDIO_SIGPROC_SRC)/SSTP \
-I$(TECHAUDIO_LIMOTOR_SRC)/API \
-I$(TECHAUDIO_LIMOTOR_COMMON_SRC) \
-I$(TECHAUDIO_LIMOTOR_SRC)

SRCS = $(TECHAUDIO_LIMOTOR_SRC)/LIMITOR_main_32f.c \
$(TECHAUDIO_LIMOTOR_SRC)/API/LIMITOR_API_32f.c \
$(TECHAUDIO_LIMOTOR_SRC)/API/LIMITOR_API_coordinate_32f.c

SRCSCPP = $(TECHAUDIO_LIMOTOR_SRC)/Test/LIMITOR_TEST_example_32f.cpp
# $(TECHAUDIO_LIMOTOR_SRC)/VST/LIMITOR_VST_audioEffect_32f.cpp \
# $(TECHAUDIO_LIMOTOR_SRC)/VST/LIMITOR_VST_main_32f.cpp

NAME = Bonjour

CC = gcc

CPP = g++

CFLAGS += $(INCLUDES)
CFLAGS += -D_X86_GCC_4_1_1_TARGET_
#CFLAGS +=-D_X86_VC8_TARGET_

RM = rm -f

OBJ= $(SRCS:.c=.o)

OBJCPP= $(SRCSCPP:.cpp=.o)

%.o: %.c
$(CC) $(CFLAGS) $<

%.o: %.cpp
$(CPP) $(CFLAGS) $<

all: $(NAME)

$(NAME): $(OBJ) $(OBJCPP)
$(LD) $^ -o $@ $(LDFLAGS)

clean:
$(RM) $(OBJ) $(OBJCPP)

fclean: clean
$(RM) $(NAME)
re: fclean all

这些是我在执行 make 时收到的错误:

/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 20 a un index de symbole 13 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): réadressage 21 a un index de symbole 22 invalide
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): réadressage 0 a un index de symbole 2 invalide
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: dans la fonction « _start »:
(.text+0x18): référence indéfinie vers « main »
/tmp/ccvLa3z6.o: dans la fonction « LIMITOR_3DSL_getStatusSize_32f »:
LIMITOR_main_32f.c:(.text+0x1e): référence indéfinie vers « MALLOC_3DSL_compute_memory_size_align_heap »

最佳答案

尝试添加这些规则:

%.o: %.c
$(CC) $(CFLAGS) $<

%.o: %.cpp
$(CPP) $(CFLAGS) $<

并将您的主线更改为:

$(NAME): $(OBJ) $(OBJCPP)
$(LD) $^ -o $@ $(LDFLAGS)

编辑:

这是将其集成到您的代码中的尝试。不过,我没有运行它。

TECHAUDIO_ROOT = ../../../../../../..
TECHAUDIO_GENERIC = $(TECHAUDIO_ROOT)/Generic
TECHAUDIO_MALLOC_3DSL = $(TECHAUDIO_ROOT)/Tools/Malloc_3DSL
TECHAUDIO_SIGPROC_COMMON_SRC = ../../../../SignalProcessing/RefFT/Common/src
TECHAUDIO_SIGPROC_SRC = ../../../../SignalProcessing/RefFT/Float/src
TECHAUDIO_LIMOTOR_SRC = ../../../RefFT/Float/src
TECHAUDIO_LIMOTOR_COMMON_SRC = ../../../RefFT/Common/src/API

INCLUDES += -I$(TECHAUDIO_GENERIC)/Common/Include \
-I$(TECHAUDIO_GENERIC)/X86/Include \
-I$(TECHAUDIO_MALLOC_3DSL)/RefFT/Common/Include \
-I$(TECHAUDIO_SIGPROC_COMMON_SRC) \
-I$(TECHAUDIO_SIGPROC_SRC)/Math \
-I$(TECHAUDIO_SIGPROC_SRC)/Transform \
-I$(TECHAUDIO_SIGPROC_SRC)/Filter \
-I$(TECHAUDIO_SIGPROC_SRC)/Vector \
-I$(TECHAUDIO_SIGPROC_SRC)/SSTP \
-I$(TECHAUDIO_LIMOTOR_SRC)/API \
-I$(TECHAUDIO_LIMOTOR_COMMON_SRC) \
-I$(TECHAUDIO_LIMOTOR_SRC)

SRCS = $(TECHAUDIO_LIMOTOR_SRC)/LIMITOR_main_32f.c \
$(TECHAUDIO_LIMOTOR_SRC)/API/LIMITOR_API_32f.c \
$(TECHAUDIO_LIMOTOR_SRC)/API/LIMITOR_API_coordinate_32f.c

SRCSCPP = $(TECHAUDIO_LIMOTOR_SRC)/Test/LIMITOR_TEST_example_32f.cpp
# $(TECHAUDIO_LIMOTOR_SRC)/VST/LIMITOR_VST_audioEffect_32f.cpp \
# $(TECHAUDIO_LIMOTOR_SRC)/VST/LIMITOR_VST_main_32f.cpp

NAME = Bonjour

CC = gcc -c
CPP = g++ -c
LD = g++ -o

CFLAGS += $(INCLUDES)
CFLAGS += -D_X86_GCC_4_1_1_TARGET_
#CFLAGS +=-D_X86_VC8_TARGET_

RM = rm -f

OBJ= $(SRCS:.c=.o)

OBJCPP= $(SRCSCPP:.cpp=.o)

%.o: %.c
$(CC) $< -o $@ $(CFLAGS)

%.o: %.cpp
$(CPP) $< -o $@ $(CFLAGS)

all: $(NAME)

$(NAME): $(OBJ) $(OBJCPP)
$(LD) $@ $^ -lm

clean:
$(RM) $(OBJ) $(OBJCPP)

fclean: clean
$(RM) $(NAME)

re: fclean all

关于c++ - 使用 gcc 和 g++ 分别编译包含 C 和 C++ 文件的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com