gpt4 book ai didi

c++ - 如何在Mac上进行编译时摆脱此错误?

转载 作者:行者123 更新时间:2023-12-02 10:52:27 24 4
gpt4 key购买 nike

早上好,
在Mac上编译项目时遇到一些问题。我正在使用Makefile进行编译,当我执行make命令时,出现了错误。问题是,在docker下,编译工作正常,但对于该项目,我需要在mac下编译,因为我使用的是未安装在docker下的库,因此我没有时间时刻。我认为这可能是因为shell使用clang进行编译,而我习惯于使用GCC。我在Mac下是个新手,所以我不知道该怎么做才能避免这种情况。

MY_INFO$ make
c++ -c -o *.o Input.cpp
In file included from Input.cpp:8:
In file included from ./Input.hpp:11:
./Error.hpp:19:9: error: exception specification of overriding function is more lax than base version
~Error() = default;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/exception:101:13: note: overridden virtual function is here
virtual ~exception() _NOEXCEPT;
^
In file included from Input.cpp:8:
In file included from ./Input.hpp:11:
./Error.hpp:19:20: warning: defaulted function definitions are a C++11 extension [-Wc++11-extensions]
~Error() = default;
^
./Error.hpp:21:17: error: exception specification of overriding function is more lax than base version
const char *what() const noexcept {return _msg;}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/exception:102:25: note: overridden virtual function is here
virtual const char* what() const _NOEXCEPT;
^
In file included from Input.cpp:8:
In file included from ./Input.hpp:11:
./Error.hpp:21:29: error: expected ';' at end of declaration list
const char *what() const noexcept {return _msg;}
^
;
./Error.hpp:18:15: error: member initializer '_msg' does not name a non-static data member or base class
: _msg(msg), _file(file), _line(line) {}
^~~~~~~~~
In file included from Input.cpp:8:
./Input.hpp:18:9: error: exception specification of overriding function is more lax than base version
~InputSoundError() = default;
^
./Error.hpp:19:9: note: overridden virtual function is here
~Error() = default;
^
In file included from Input.cpp:8:
./Input.hpp:18:30: warning: defaulted function definitions are a C++11 extension [-Wc++11-extensions]
~InputSoundError() = default;
^
2 warnings and 5 errors generated.
make: *** [*.o] Error 1

MY_INFO$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
如下,我的makefile。
SRC     =   *.cpp
OBJ = $(foreach source, $(SRC), $(source:.cpp=.o))
NAME = SoundTest
CFLAGS = -g -Wall -Wextra -std=c++11
LDFLAGS = -lportaudio
CC = g++
LD = g++

all: $(NAME)

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

obj/%.o: %.cpp
$(CC) -o $@ -c $^ $(CFLAGS)

clean:
rm -rf $(OBJ)

fclean: clean
rm -rf $(NAME)

re: fclean all

最佳答案

您的makefile有一些问题。首先,您不能使用*.cpp来显示文件名-make具有自己的功能,因此您应该使用...

SRC = $(wildcard *.cpp)
您也可以使用类似...的方法检查 SRC变量的值。
$(info SRC=[$(SRC)])
接下来,构建目标文件的规则是...
obj/%.o: %.cpp
$(CC) -o $@ -c $^ $(CFLAGS)
因此,对于任何给定的源文件 foo.cpp,此规则告诉make如何从该源文件构建 obj/foo.o。但是您可以使用...根据源文件名指定目标文件列表。
OBJ = $(foreach source, $(SRC), $(source:.cpp=.o))
因此,如果 SRC是,例如 a.cpp b.cpp,然后 OBJa.o b.o而不是 obj/a.o obj/b.o,这是使用 obj/%.o: %.cpp规则所必需的。您真正想要的是将 OBJ设为 obj/a.o obj/b.o(例如),因此变量定义应为...
OBJ = $(patsubst %.cpp,obj/%.o,$(SRC))
将这些位放在一起可以得到...
SRC     =   $(wildcard *.cpp)
OBJ = $(patsubst %.cpp,obj/%.o,$(SRC))
NAME = SoundTest
CFLAGS = -g -Wall -Wextra -std=c++11
LDFLAGS = -lportaudio
CC = g++
LD = g++

all: $(NAME)

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

obj/%.o: %.cpp
$(CC) -o $@ -c $^ $(CFLAGS)

clean:
rm -rf $(OBJ)

fclean: clean
rm -rf $(NAME)

re: fclean all

关于c++ - 如何在Mac上进行编译时摆脱此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64088596/

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