I guess this is embarrassing if I told you I cant get this to compile. would you please help me:
#include<memory>
using namespace std;
int main()
{
std::unique_ptr<int> p1(new int(5));
return 0;
}
$ gcc main.cpp
main.cpp: In function ‘int main()’:
main.cpp:6:2: error: ‘unique_ptr’ was not declared in this scope
main.cpp:6:13: error: expected primary-expression before ‘int’
main.cpp:6:13: error: expected ‘;’ before ‘int’
$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
This is just a guess.
Most likely you compiled your program like this (or similarly) :
g++ main.cpp
If you did, then the problem is that g++ uses c++03 as default. To use c++11 features (and std::unique_ptr), you need to use newer version of c++ :
g++ -std=c++11
or
g++ -std=c++14
and I would recommend to use also -Wall -Wextra -pedantic.
If you are using Code::Blocks, go to Settings > Compiler > Global compiler settings > Compiler settings and look for the Have g++ follow the C++11 ISO C++ language standard [-std=c++11] and check it!
(Code::Blocks will add the -std=c++11 for you when compiling)
Related
I try to compile this very simplified program:
#include <pthread.h>
int main(){
pthread_yield();
return 0;
}
using -pthread like the IBM side says:
$ g++ -pthread test.cpp -o test
and get this error:
test.cpp: In function 'int main()':
test.cpp:4:15: error: 'pthread_yield' was not declared in this scope
pthread_yield();
I tried lots of other falgs too, but nothing worked so far. The pthread.h is in /usr/includes but pthread_yield() needs _AIX_PTHREADS_D7 defined.
Do I have to define this myselfe or is this done by adding some flag?
THX!
Other than defining symbol _AIX_PTHREADS_D7 you have to use library libpthreads_compat as well.
g++ -o marscode marscode.cc -D_AIX_PTHREADS_D7 -lpthreads_compat -lpthreads
I have been programming using Gtkmm for a while now, using C++11 features without problems. Today, I added a line of code using the C++14 feature std::make_unique and got a compiler error. At first, I thought I had an issue with my build configuration but after some testing, I narrowed it down to Gtkmm. Here is code that builds fine on my system:
Build command:
g++ -std=c++14 main.cpp
Code:
#include <memory>
int main()
{
std::unique_ptr<int> intPtr;
intPtr = std::make_unique<int>(3);
return 0;
}
If I switch to this build command:
g++ -std=c++14 main.cpp `pkg-config gtkmm-3.0 --cflags --libs`
The code no longer builds. I get the following errors:
main.cpp: In function ‘int main()’:
main.cpp:7:14: error: ‘make_unique’ is not a member of ‘std’
intPtr = std::make_unique<int>(3);
^
main.cpp:7:31: error: expected primary-expression before ‘int’
intPtr = std::make_unique<int>(3);
What's the problem with Gtkmm? For your information, I am using g++ v 5.4.0 and gtkmm 3.0.
EDIT: It seems this is not C++14 related. I tried building with other C++14 features, like [[DEPRECATED]] and it worked fine. Maybe only the standard library... I also tried switching to g++ 7 and got the same errors.
You are probably running into a problem with the C++ standard that is specified by the --cflags argument in pkg-config gtkmm-3.0 --cflags --lib. If -std=c++11 or something older is the result of providing the --cflags option, then it will override any earlier specifications. You can probably fix the problem just by placing your desired specification at the end:
g++ main.cpp `pkg-config gtkmm-3.0 --cflags --libs` -std=c++14
Following line compiles successfully on g++ but gives error on clang::
static_assert(tBits <= sizeof(ULONG)*8, "This is IO method");
g++ warning ::
there are no arguments to 'static_assert' that depend on a template parameter, so a declaration of 'static_assert' must be available
clang error ::
use of undeclared identifier 'static_assert'; did you mean 'static_cast'?
please help me out.
Function declaration from comment:
template < size_t tBits >
HRESULT DoIO( std::bitset< tBits >& bitsetToSerialize ) const
"static_assert" was introduced in C++11 as a language keyword - not a function or a macro.
Both compilers are giving you the "I don't know this function" warnings/errors.
For the compiler to give you "I don't know this function" when you are using "static_assert", the compiler must not be compiling with C++11 support (-std=c++11).
To demonstrate this, I took the following piece of code:
#include <bitset>
template<size_t tBits>
int DoIO(std::bitset<tBits>& /*bitsetToSerialize*/)
{
static_assert(tBits <= sizeof(unsigned long) * 8, "tBits is too big.");
return tBits;
}
Then I compiled it with GCC 4.7.3 and I got the following error:
osmith#olivia64 ~/src $ g++ -o sa.o -c sa.cpp
sa.cpp: In function ‘int DoIO(std::bitset<_Nb>&)’:
sa.cpp:6:78: error: there are no arguments to ‘static_assert’ that depend on a template parameter, so a declaration of ‘static_assert’ must be available [-fpermissive]
sa.cpp:6:78: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
Then I compiled it with C++11 support enabled and it compiled without a problem:
osmith#olivia64 ~/src $ g++ -std=c++11 -o sa.o -c sa.cpp -Wall
osmith#olivia64 ~/src $
So, then I compiled it with Clang
osmith#olivia64 ~/src $ clang++ -o sa.o -c sa.cpp
sa.cpp:6:9: error: use of undeclared identifier 'static_assert'; did you mean 'static_cast'?
static_assert(tBits <= sizeof(unsigned long) * 8, "tBits is too big.");
^
1 error generated.
and finally I compiled it using Clang with C++11 support, where it compiled fine.
osmith#olivia64 ~/src $ clang --version
Ubuntu clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
osmith#olivia64 ~/src $ clang++ -std=c++11 -o sa.o -c sa.cpp
osmith#olivia64 ~/src $
Just to be sure, let's give the compiler opportunity to help us and turn on "-Wall":
osmith#olivia64 ~/src $ g++ -Wall -o sa.o -c sa.cpp
sa.cpp:6:9: warning: identifier ‘static_assert’ is a keyword in C++11 [-Wc++0x-compat]
sa.cpp: In function ‘int DoIO(std::bitset<_Nb>&)’:
sa.cpp:6:78: error: there are no arguments to ‘static_assert’ that depend on a template parameter, so a declaration of ‘static_assert’ must be available [-fpermissive]
sa.cpp:6:78: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
I am trying to compile my code using libboost library, by putting #include <boost/python.hpp> in my C++ code. Could somebody please help me the right command to run this, especially to include and link the library. I'm pretty basic in this.
The command used(but not working):
g++ try.cpp -L /usr/lib/libboost_python.so -o try
EDIT:
The tested code:
#include <boost/python.hpp>
#include <iostream>
int main()
{
std::cout << "Yes, it works :-)" << std::endl;
return 0;
}
Error messages:
from try.cpp:1:
/usr/include/boost/python/enum.hpp:31: error: expected ‘;’ before ‘*’ token
/usr/include/boost/python/enum.hpp:32: error: expected ‘;’ before ‘(’ token
/usr/include/boost/python/enum.hpp:33: error: ‘PyObject’ has not been declared
/usr/include/boost/python/enum.hpp:52: error: expected constructor, destructor, or type conversion before ‘*’ token
/usr/include/boost/python/enum.hpp:67: error: ‘void* boost::python::enum_<T>::convertible_from_python’ is not a static member of ‘struct boost::python::enum_<T>’
/usr/include/boost/python/enum.hpp:67: error: template definition of non-template ‘ void* boost::python::enum_<T>::convertible_from_python’
/usr/include/boost/python/enum.hpp:67: error: ‘PyObject’ was not declared in this scope
/usr/include/boost/python/enum.hpp:67: error: ‘obj’ was not declared in this scope
/usr/include/boost/python/enum.hpp:80: error: variable or field ‘construct’ declared void
/usr/include/boost/python/enum.hpp:80: error: ‘PyObject’ was not declared in this scope
/usr/include/boost/python/enum.hpp:80: error: ‘obj’ was not declared in this scope
/usr/include/boost/python/enum.hpp:80: error: expected primary-expression before ‘*’ token
/usr/include/boost/python/enum.hpp:80: error: ‘data’ was not declared in this scope
Another thing is that when I compile g++ -Wall thread_one.cpp -o thread_one -lboost_thread, that works in order to use boost_thread library.
Try this:
g++ try.cpp -o try -lboost_python
It'd be good style to also add -W -Wall -Wextra -pedantic to your compiler invocation (so that your next SO question can be more specific :-)). Also, -O2 or -O3 for optimization is probably a very good idea, especially with Boost. Finally, splitting the building up into separate stages makes recompiling faster when you have multiple files:
g++ -c -o try.o try.cpp -W -Wall -Wextra -pedantic -O2
g++ -o try try.o -s -lboost_python
Finally, it is working. The command used is as below:
g++ -I/usr/include/python2.6 try.cpp -o try -lboost_python -lpython2.6
I've come across a really weird error that only pops up if I use the ansi flag.
#include <memory>
class Test
{
public:
explicit Test(std::shared_ptr<double> ptr) {}
};
Here's the compilation, tested with gcc 4.5.2 and 4.6.0 (20101127):
g++ -std=c++0x -Wall -pedantic -ansi test.cpp
test.cpp:6:34: error: expected ')' before '<' token
But compiling without -ansi works. Why?
For the GNU C++ compiler, -ansi is another name for -std=c++98, which overrides the -std=c++0x you had earlier on the command line. You probably want just
$ g++ -std=c++0x -Wall minimal.cpp
(-pedantic is on by default for C++, so it's unnecessary to say it again. If you want pickier warnings, try adding -Wextra.)
std::shared_ptr doesn't exist in c++98. Try these changes:
#include <tr1/memory>
...
explicit Test(std::tr1::shared_ptr<double> ptr) {}
Um, because there is not yet an ANSI standard for C++0x? The ANSI flag checks for conformance with existing standards, not future ones.