Compile pthread.h stuff on AIX using g++ - c++

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

Related

How to use boost asio library with Cygwin 64 bit

I am running Windows 10 64bit. Cygwin is 64 bit.
I installed boost from cygwin package manager.
I tried to compile test.cpp:
#include <boost/asio.hpp>
int
main(int argc, char**argv)
{
return 0;
}
using command
g++ -std=c++11 -Wall -g -D__USE_W32_SOCKETS D_WIN32_WINNT=_WIN32_WINNT_WIN7 test.cpp -o test.exe
but compile fails. It looks like posix is being used.
Any ideas why this fails?
In file included from /usr/include/boost/asio/detail/fd_set_adapter.hpp:22:0,
from /usr/include/boost/asio/detail/select_reactor.hpp:27,
from /usr/include/boost/asio/detail/reactor.hpp:29,
from /usr/include/boost/asio/detail/impl/task_io_service.ipp:24,
from /usr/include/boost/asio/detail/task_io_service.hpp:198,
from /usr/include/boost/asio/impl/io_service.hpp:71,
from /usr/include/boost/asio/io_service.hpp:767,
from /usr/include/boost/asio/basic_io_object.hpp:19,
from /usr/include/boost/asio/basic_socket.hpp:20,
from /usr/include/boost/asio/basic_datagram_socket.hpp:20,
from /usr/include/boost/asio.hpp:21,
from appcontrol.cpp:16:
/usr/include/boost/asio/detail/posix_fd_set_adapter.hpp:82:12: error: 'fd_set' does not name a type
operator fd_set*()
^
/usr/include/boost/asio/detail/posix_fd_set_adapter.hpp:105:11: error: 'fd_set' does not name a type
mutable fd_set fd_set_;
^
/usr/include/boost/asio/detail/posix_fd_set_adapter.hpp: In constructor 'boost::asio::detail::posix_fd_set_adapter::posix_fd_set_adapter()':
/usr/include/boost/asio/detail/posix_fd_set_adapter.hpp:42:14: error: 'fd_set_' was not declared in this scope
FD_ZERO(&fd_set_);
^
Thanks to help from #cygwin IRC:
g++ -std=c++11 -Wall -g -D_XOPEN_SOURCE=500 test.cpp -o test.exe -lboost_system

Clangs C++ Module TS support: How to tell clang++ where to find the module file?

In his talk at CppCon, Richard Smith mentioned that even though the Module TS support is currently work in progress, it can already be used. So I build clang 4.0 from svn and tried it on a very simple example. In my myclass.cppm file I defined a simple wrapper for an int
module myclass;
export class MyClass {
public:
MyClass (int i)
: _i{i} {}
int get() {
return _i;
}
private:
int _i;
};
and my main.cpp just creates one instance of that class and outputs its held int to std::cout.
#include <iostream>
#include <string>
import myclass;
int main(int, char**) {
MyClass three{3};
std::cout << std::to_string(three.get()) << std::endl;
}
Then I tried to compile it via clang++ -std=c++1z -fmodules-ts main.cpp and with clang++ -std=c++1z -fmodules-ts myclass.cppm main.cpp but that doesn`t work and I get the same error message in both cases:
main.cpp:3:8: fatal error: module 'myclass' not found
import test.myclass;
~~~~~~~^~~~
1 error generated.
Unfortunately I have not been able to find documentation for -fmodules-ts. Does someone know how I can get clang++ to compile my simple example?
You can compile it as follows:
clang++ -std=c++1z -fmodules-ts --precompile -o myclass.pcm myclass.cppm
clang++ -std=c++1z -fmodules-ts -fmodule-file=myclass.pcm -o modules_test main.cpp
However, this can't be how it's meant to work since you'd manually need to encode the dependency hierarchy of your modules in the calls to the compiler; I'd be very interested in the correct answer to this question :-).
-fprebuilt-module-path works even though it fires a warning: "argument unused during compilation: '-fprebuilt-module-path=.'"
The complete command was:
clang++-4.0 -std=c++1z -fmodules-ts -fprebuilt-module-path=. -o modules_test main.cpp
as of 27th of December, 2017 I have checked out the latest llvm branch, built it on my macbook and then eexecuted the following:
./../bin/clang++ -std=c++17 -fmodules-ts --precompile -o myclass.pcm myclass.cppm
./../bin/clang++ -std=c++17 -fmodules-ts -c myclass.pcm -o myclass.o
./../bin/clang++ -std=c++17 -fmodules-ts -fprebuilt-module-path=. -o main main.cpp hello.o
and tada, worked prefectly without any warnings or errors.

unique_ptr compile error

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)

Using exit() in c++

For one reason or another, I am messing around with the exit() function in c++. I am getting all kinds of strange errors from my mac running lion (64 bit). I am compiling using g++ -o -g -Wall.
Exhibit A:
#include <iostream>
int main(int arc, char *argv[]){
exit(1);
}
The Terminal output looks like this
$ g++ -o -g -Wall test main.cpp
ld: in test, can't link with a main executable for architecture x86_64
collect2: ld returned 1 exit status
but $ g++ -o test main.cpp compiles fine.
using #include<stdio.h> or #include<stdlib.h> result in the same compilation error.
I am just wondering if anyone might be able to see immediately what is going on here?
test is the name of the binary to produce, your first argument list should be:
> g++ -g -Wall -o test main.cpp
^^^^^^^ -o has test for an argument
-o is meant to be followed immediately by the name of the output file. It is probably trying to use your old binary 'test' as a source file, incorrectly.
Try this:
g++ -o test -g -Wall main.cpp

atomic_thread_fence is not a member of std

Compiling with:
g++ -std=c++0x -Wall -pthread test2.cc -o hello
I'm getting this error:
test2.cc: atomic_thread_fence is not a member of std
I am using g++ 4.5.2 in Ubuntu. What am I missing?
Do you have the following in test2.cc?
#include <atomic>