Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 310
galois Avatar asked Nov 26 '25 21:11

galois


1 Answers

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.

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

N.B. 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.

N.B. instead of linking to -lpthread you should compile and link with -pthread

N.B. 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.

like image 161
Jonathan Wakely Avatar answered Nov 29 '25 15:11

Jonathan Wakely



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!