【发布时间】:2021-01-13 05:37:27
【问题描述】:
早上好,
我在 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
【问题讨论】:
-
std::cout << "C++ is " << __cplusplus << "\n";输出 201703 吗? -
@Eljay 输出为
199711 -
我不认为你的
obj/%.o: %.cpp规则正在被使用,因为你实际上是在尝试从foo.cpp构建foo.o——而不是obj/foo.o。在这种情况下,正在使用未使用CFLAGS的内置规则。尝试将OBJ的定义更改为OBJ = $(patsubst %.cpp,obj/%.o,$(SRC))。还将SRC的定义更改为SRC = $(wildcard *.cpp)。
标签: c++ gcc compiler-errors clang