有人可以解释下面如何处理2个代码片段的区别吗?它们肯定会编译成不同的汇编代码,但我试图理解代码的行为方式可能不同.我知道字符串文字被抛入只读内存并且实际上是静态的,但是它与下面的显式静态有何不同?
struct Obj1
{
void Foo()
{
const char* str( "hello" );
}
};
Run Code Online (Sandbox Code Playgroud)
和
struct Obj2
{
void Foo()
{
static const char* str( "hello" );
}
};
Run Code Online (Sandbox Code Playgroud) 这是我目前的makefile.
CXX = g++
CXXFLAGS = -Wall -O3
LDFLAGS =
TARGET = testcpp
SRCS = main.cpp object.cpp foo.cpp
OBJS = $(SRCS:.cpp=.o)
DEPS = $(SRCS:.cpp=.d)
.PHONY: clean all
all: $(TARGET)
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJS) -o $(TARGET)
.cpp.o:
$(CXX) $(CXXFLAGS) -c $< -o $@
%.d: %.cpp
$(CXX) -M $(CXXFLAGS) $< > $@
clean:
rm -f $(OBJS) $(DEPS) $(TARGET)
-include $(DEPS)
Run Code Online (Sandbox Code Playgroud)
它有一个例外,完美地工作.如果目录已经是干净的(没有*.d,*.o)并且我运行'make clean',它会重新创建依赖项,然后立即删除它们:
[user@server proj]$ make
g++ -M -Wall -O3 foo.cpp > foo.d
g++ -M -Wall -O3 object.cpp > object.d
g++ …Run Code Online (Sandbox Code Playgroud)