簡體   English   中英

CFLAGS = -std = c ++ 11 -O3 -Wall -pedantic在makefile中無法識別

[英]CFLAGS=-std=c++11 -O3 -Wall -pedantic not being recognized in makefile

我有一個makefile,可在其中編譯許多cpp文件以制作不同的可執行文件。

CFLAGS=-std=c++11 -O3 -Wall -pedantic                                                                                                                                                                              

CC=g++1

LDFLAGS=

all: auto_variables explict_kw for_each functor member_initializer member_initializer2 reference ref_ptr smart_pointers vector

auto_variables : auto_variables.o
    $(CC) $(CFLAGS) auto_variables.o $(LDFLAGS) -o $@

explict_kw : explict_kw.o
    $(CC) $(CFLAGS) explict_kw.o $(LDFLAGS) -o $@

for_each : for_each o
    $(CC) $(CFLAGS) for_each.o $(LDFLAGS) -o $@

functor : functor.o
    $(CC) $(CFLAGS) functor.o $(LDFLAGS) -o $@

member_initializer : member_initializer.o
    $(CC) $(CFLAGS) member_initializer.o $(LDFLAGS) -o $@

member_initializer2 : member_initializer2.o
    $(CC) $(CFLAGS) member_initializer2.o $(LDFLAGS) -o $@

reference : reference.o
    $(CC) $(CFLAGS) reference.o $(LDFLAGS) -o $@

ref_ptr : ref_ptr.o
    $(CC) $(CFLAGS) ref_ptr.o $(LDFLAGS) -o $@

smart_pointers : smart_pointers.o
    $(CC) $(CFLAGS) smart_pointers.o $(LDFLAGS) -o $@

vector : vector.o
    $(CC) $(CFLAGS) vector.o $(LDFLAGS) -o $@

clean:
    rm -rf auto_variables explict_kw for_each functor member_initializer member_initializer2 reference ref_ptr smart_pointers vector 
    rm -rf *.o 

我已將-std=c++11指定為CFLAGS但鍵入make時似乎沒有采用該選項。

g++    -c -o auto_variables.o auto_variables.cpp
auto_variables.cpp:8:27: error: ISO C++ forbids declaration of ‘sum’ with no type [-fpermissive]
 auto sum(int x, int y) -> int
                           ^
auto_variables.cpp:8:27: error: top-level declaration of ‘sum’ specifies ‘auto’
auto_variables.cpp:8:27: error: trailing return type only available with -std=c++11 or -std=gnu++11
auto_variables.cpp: In function ‘int main()’:
auto_variables.cpp:16:8: error: ‘var_1’ does not name a type
   auto var_1 = 5;
        ^
auto_variables.cpp:18:8: error: ‘var_2’ does not name a type
   auto var_2 = 'c';
        ^
auto_variables.cpp:20:16: error: ‘var_1’ was not declared in this scope
   std::cout << var_1 << std::endl;
                ^
auto_variables.cpp:21:16: error: ‘var_2’ was not declared in this scope
   std::cout << var_2 << std::endl;
                ^
auto_variables.cpp:23:8: error: ‘fun_sum’ does not name a type
   auto fun_sum = [](int a, int b)
        ^
auto_variables.cpp:46:8: error: ‘itr’ does not name a type
   auto itr = mapofStrs.begin();
        ^
auto_variables.cpp:47:9: error: ‘itr’ was not declared in this scope
   while(itr != mapofStrs.end())
         ^
<builtin>: recipe for target 'auto_variables.o' failed
make: *** [auto_variables.o] Error 1

為什么會這樣呢?

我建議使用CXXFLAGS代替CFLAGS

CFLAGS用於C標志,而CXXFLAGS用於C ++標志。

CXXFLAGS=-std=c++11 -O3 -Wall -pedantic

另外,我不清楚您的意思是:

CC=g++1

那是錯字嗎? 我懷疑你打算用

CC=g++

與之前的更改保持一致,您應該將其更改為:

CXX=g++

然后,將使用$(CC) $(CFLAS)所有位置更改為$(CXX) $(CXXFLAGS)

發生這種情況是因為您濫用了Make。

對於C ++,請使用GNU Make頁面上記錄的正確變量: CXXFLAGS

CXXFLAGS=-std=c++11 -O3 -Wall -pedantic

至少一開始您不需要設置其他任何內容。

這將確保您使用正確的隱式規則。 如果這樣做沒有幫助,請制作一個最小,完整且可驗證的示例 ,其中還會顯示代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM