简体   繁体   中英

How to include -std=c++11 and -lpthread in makefile?

I tried the advice in this answer , but it's for GCC and didn't help anyways.

I want to #include <thread> in a file, so I have a make file as the following:

OBJS    = clitest.o Sources/NClient.o
CC      = g++
DEBUG   = -g
CFLAGS  = -Wall -c $(DEBUG)
LFLAGS  = -Wall $(DEBUG)

clitest: $(OBJS)
    $(CC) $(LFLAGS) $(OBJS) -o clitest

Where should I include -std=c++11 and -lpthread in this? I've tried just about every combination I can, but I continue to get this error when I run make :

/usr/include/c++/4.8.3/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

I believe this is the command it's running?

[jaska@localhost NClient]make
g++    -c -o clitest.o clitest.cpp

Here's the source code file, too:

#include <thread>
#include <string>

void task(std::string msg){
    std::cout << msg << '\n';
}

...
...

std::thread t1(task, "message");
client->create();
t1.join();

Your makefile doesn't have an explicit rule for compiling the objects in $(OBJS) so that means the implicit rule will be used, which is what produces this command:

g++    -c -o clitest.o clitest.cpp

The implicit rule for turning a .cpp file into a .o file is approximately:

$(CXX) $(CXXFLAGS) -c -o $@ $<

so to add options that affect that rule you should add them to the CXXFLAGS variable ( CFLAGS is conventionally used for compiling C files, and CC is the C compiler, the conventional variable for the C++ compiler is CXX ).

The -lpthread option is a linker option, so need to be given during linking. You have defined your own rule for linking, so you should either add -lpthread to that recipe or add it to the LFLAGS variable.

NB using -Wall and -g during linking is useless, they have no effect.

NB adding -c to the CFLAGS is wrong, the implicit rules for compiling .c files already include -c , just as the implicit one for C++ files includes -c . This doesn't cause any problems because the CFLAGS variable is not used by your makefile, or by the implicit rule for compiling .cpp files.

NB instead of linking to -lpthread you should compile and link with -pthread

NB the implicit rule for linking, and makefile convention, is to use the variable LDFLAGS for linker options, and LIBS for libraries such as -lpthread , so I would rewrite your makefile as:

OBJS    = clitest.o Sources/NClient.o
CXX     = g++
DEBUG   = -g
CXXFLAGS  = -Wall $(DEBUG) -std=c++11 -pthread
LDFLAGS = -pthread

clitest: $(OBJS)
    $(CXX) $(LDFLAGS) -o $@ $^ $(LIBS)

This still relies on the implicit rule for turning the .cpp files into .o files, but now that implicit rule will pick up the right options from the CXXFLAGS variable.

Add to CFLAGS and LFLAGS

CFLAGS  = -Wall -c $(DEBUG) -std=c++11 -pthread
LFLAGS  = -Wall $(DEBUG) -std=c++11 -pthread

that should be enough.

For implicit rules also overwrite the CXXFLAGS variable

CXXFLAGS  = -Wall -c $(DEBUG) -std=c++11 -pthread

As you see from your command line output

 [jaska@localhost NClient]make g++ -c -o clitest.o clitest.cpp 

the other flags from CFLAGS also weren't used for compiling.

They are effectively command line parameters to gcc/g++ or ld, the linker. So they must be passed as additions to CFLAGS and LFLAGS (or possibly as CXXFLAGS, CPPFLAGS, LDFLAGS depending on how the Makefile functions). Your example does not show you passing CFLAGS to the C++ compiler though, so that is also problematic. Pass it explicitly or set the CXXFLAGS for your C++ compiler (C is not C++).

I would suggest you resolve your problem initially on the command line and then when it works move on to the Makefile. Effectively you're trying to get Make to create the line:

g++ -Wall -g -std=c++11 -pthread clitest.o Sources/NCLient.o -o clitest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM