I get a compile error when trying to create an object file from a compiled source file. I am using the header which came with c++11. I am also using a c++ pattern recognition library with several other includes.
All I did was add #include <thread> to my rbm_test.cc source file.
My compile command:
g++ -std=c++11 -O3 -DQUIET -fPIC -pthread -ansi -pedantic -DARCH_INTEL -Wall -W -Wchar-subscripts -Wpointer-arith -Wcast-qual -Wwrite-strings -Wconversion -Wno-old-style-cast -Wctor-dtor-privacy -Wnon-virtual-dtor -I../src -I../.. -DPATREC -D_UNIX_ -o rbm_test.o -c ../src/rbm_test.cc
The compile error I get is:
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.
Strangely, when I compile the following code example with
g++ -std=c++11 -pthread -c main.cpp -o main.o
then I have no error.
Here Is main.cpp
#include <iostream>
#include <thread>
void f1()
{
std::cout << "Thread executing\n";
}
int main()
{
std::thread t1(f1);
std::thread t2(f1);
t1.join();
t2.join();
}
Is it possible that some of the compile flags are conflicting when I try to compile rbm_test.cc?
The -ansi flag conflicts with the -std=c++11 flag. -ansi is equivalent to -std=c++98. Removing the -ansi flag solves the problem.
Related
my gcc version is the following:
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
while clang
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
but the following code doesn't compile:
#include <thread>
#include <vector>
int main(){
std::vector<std::jthread> myJThreads;
return 0;
}
I've tried these so far:
g++ -O2 -std=c++2a -Wall -Wextra -Wpedantic -lm -lstdc++ example.cpp -pthread -o example
clang -O2 -std=c++2a -Wall -Wextra -Wpedantic -lm -lstdc++ example.cpp -pthread -o example
clang++-12 -O2 -std=c++2a -Wall -Wextra -Wpedantic -lm -lstdc++ example.cpp -pthread -o example
getting:
error: ‘jthread’ is not a member of ‘std’;
error: no member named 'jthread' in namespace 'std'
how can I fix this?
I am attempting to compile a program that uses ranged based for loops, and a couple other features available only in c++11 and above. When I attempt to compile the program using a makefile in the terminal, I get this error:
error: range-based ‘for’ loops are not allowed in C++98 mode
and some warnings:
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
What's annoying is that this has happened before, but it just resolved itself. However, this time it has not resolved itself.
Here's my makefile:
main: main.o
g++5 -std=c++11 -Wall -Werror -g *.cpp -o lab4
Here are some examples of things I have tried to change in the makefile, but did not work.
g++-5 -std=c++11 -Wall -Werror -g *.cpp -o lab4
g++5 -std=gnu++11 -Wall -Werror -g *.cpp -o lab4
g++5 -std=c++0x -Wall -Werror -g *.cpp -o lab4
g++ -std=c++11 -Wall -Werror -g *.cpp -o lab4
g++ -std=gnu++11 -Wall -Werror -g *.cpp -o lab4
All of the previous examples result in similar warnings and errors. What can I do to fix the problem?
I am using LLVM's LLD8 to replace GNU-LD to improve link speed. But when I make .so file, there is a strange error in the release mode. After streamlining, I reproduced this problem with a simple example
test.h:
#ifndef TEST_H
#define TEST_H
inline int func(){
static __thread int i = 1;
return i;
}
void handle();
#endif
test.cpp
#include <iostream>
#include <stdio.h>
#include "test.h"
void handle() {
std::cout << func() << std::endl;
}
main.cpp
#include "test.h"
void x(){
handle();
}
following cmds copied from make V=1 stdout in my project, i save those cmds into a script and compile my example file
compile lib:
$dpath/bin/g++ -DLT_OBJDIR=\".libs/\" -I. -I$dpath/include/ -w -std=gnu++11 -g -O2 -D_GLIBCXX_USE_CXX11_ABI=0 -D_NO_EXCEPTION -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DNDEBUG -Wall -Werror -Wextra -Wunused-parameter -Wformat -Wconversion -Wno-deprecated -Wno-invalid-offsetof -finline-functions -fno-strict-aliasing -mtune=core2 -Wno-psabi -Wno-sign-compare -Wno-literal-suffix -DGCC_52 -DUSE_POSIX_FALLOCATE -DSUPPORT_SSE4_2 -DHAVE_SCHED_GETCPU -DHAVE_REALTIME_COARSE -DHAVE_FALLOCATE -c test.cpp -fPIC -DPIC -o .libs/test.o
link lib
ar cru .libs/lib.a .libs/test.o
ranlib .libs/lib.a
compile files
$dpath/bin/g++ -DLT_OBJDIR=\".libs/\" -I. -I$dpath/include -std=gnu++11 -g -O2 -D_GLIBCXX_USE_CXX11_ABI=0 -D_NO_EXCEPTION -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DNDEBUG -Wall -Werror -Wextra -Wunused-parameter -Wformat -Wconversion -Wno-deprecated -Wno-invalid-offsetof -finline-functions -fno-strict-aliasing -mtune=core2 -Wno-psabi -Wno-sign-compare -Wno-literal-suffix -DGCC_52 -DUSE_POSIX_FALLOCATE -DSUPPORT_SSE4_2 -DHAVE_SCHED_GETCPU -DHAVE_REALTIME_COARSE -DHAVE_FALLOCATE -c main.cpp -fPIC -DPIC -o .libs/main.o
link so
$dpath/bin/ld.lld -shared -nostdlib .libs/main.o -l:lib.a -L./.libs -L/usr/lib64 -L$glib/gcc/x86_64-unknown-linux-gnu/5.2.0 -L$glib/gcc -L$glib/../lib64 -L$glib -L$dpath/lib/libstdc++.so --allow-shlib-undefined -soname server.so.0 -o ./server.so.0.0.0
finally i got this error
ld.lld: error: can't create dynamic relocation R_X86_64_DTPOFF32 against symbol: func()::i in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
>>> defined in ./.libs/lib.a(test.o)
>>> referenced by test.cpp:6
>>> test.o:(handle()) in archive ./.libs/lib.a
as you seen, I have already pass -fPIC in my cmds, so i pass -znotext to lld
but i got another error
ld.lld: error: relocation R_X86_64_DTPOFF32 cannot be used against symbol func()::i; recompile with -fPIC
>>> defined in ./.libs/lib.a(test.o)
>>> referenced by test.cpp:6`enter code here`
>>> test.o:(handle()) in archive ./.libs/lib.a
This error only reported when I compile my project in release mode, when i remove -O2, it links well.
But when i use the so/bin which linked in debug mode to run testcases, i got a lot of unexcept core dumps around codes which use function scoped static tls.
$ make
clang++ -o build/blist.exe src/driver.cpp src/BList.h -O0 -g -Wall -Wno-unused-parameter -Wextra -Wconversion -Wold-style-cast -std=c++14 -pedantic -Wold-style-cast
clang: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated [-Wdeprecated]
clang: error: cannot specify -o when generating multiple output files
My template implementation is in BList.cpp, but BList.h includes BList.cpp. That's why I pass the header in as an object. I don't know how to set clang to compile!
My header must be named "BList.h" according to my professor.
These parameters compiles with GCC, but not with Clang.
The error has nothing to do with including BList.cpp in BList.h (though that's a dubious practice by itself).
The problem is that you pass src/BList.h to Clang as if it was a source file. The build instruction should be:
clang++ -o build/blist.exe src/driver.cpp -O0 -g -Wall -Wno-unused-parameter -Wextra -Wconversion -Wold-style-cast -std=c++14 -pedantic -Wold-style-cast
You should update your makefile accordingly.
I'm trying to set windows programming enviroment for c++. I use the Visual Studio to write the code but my University wants me to use g++ compiler. So far I managed to link g++ with the PATH using cmd but that's not all. I have to use an alias which in linux is:
p1++="g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow
I tried the same using the command doskey:
doskey p1=g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow
and it works but whenever I use for example:
p1++ hello.cpp
it says
g++: fatal error: no input files
compilation terminated.
but if i use:
g++ hello.cc
It does compile, so my question is what does all that code mean and how can I get rid of that error?
Thanks
test.cpp (assumed to exist at C:\test.cpp):
#include <iostream>
int main(void) { std::cout << "hello" << std::endl; return 0; }
Here's what it looks like in my Windows CMD prompt:
NOTE: No quotes in alias, and it is assumed that g++ is already in your PATH.
C:\>set p1++=g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow
C:\>%p1++%
g++: fatal error: no input files
compilation terminated.
C:\>%p1++% test.cpp
C:\>a.exe
hello
C:\>doskey p1++=%p1++% $*
C:\>p1++ test.cpp
C:\>a.exe
hello
If you wanted to have the %p1++% environment variable persist across CMD prompts then you'd need to add an environment variable to your Windows User Profile.
On Windows 7:
Control Panel => System => Advanced System Settings => Environment Variables.
Create a new User Variable with name = p1++ and value = g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow.