How to run Pthreads on windows - c

I used to use a mac to write some C programs but it's not working now.
I have to use an old windows laptop for a while.
I installed codeblocks and tested a simple program using Pthreads. Unfortunately it didn't work.
pthread_create(&thrd1, NULL, thread_execute, (void *)t);
It keeps saying undefined reference to _imp__pthread_create
How can i fix it?

You've clearly got a version of pthreads for Windows. You just haven't included the .lib file in your linker settings. Do that and you should be golden.

You need to grab pthreads-win32 as pthreads is a Unix component not a Windows one.

If you are using MinGW you can MinGW installation manager and install packages that need to execute pthreads and openmp related tasks. Here is the procedure.
After opening the installation manager go to all packages and select the select packages named using mingw32-pthreads-w32 and select them for installation.
Then go to the installation -> Apply changes to install new packages. The you can use pthread.h and omp.h inside your c or c++ program without any problem.

This code works fine in an MSYS2 terminal on Windows.
All you need to do is to install gcc. (See further below.)
// hello.c
#include <omp.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *print_hello(void *thrd_nr) {
printf("Hello World. - It's me, thread #%ld\n", (long)thrd_nr);
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
printf(" Hello C code!\n");
const int NR_THRDS = omp_get_max_threads();
pthread_t threads[NR_THRDS];
for(int t=0;t<NR_THRDS;t++) {
printf("In main: creating thread %d\n", t);
pthread_create(&threads[t], NULL, print_hello, (void *)(long)t);
}
for(int t=0;t<NR_THRDS;t++) {
pthread_join(threads[t], NULL);
}
printf("After join: I am always last. Byebye!\n");
return EXIT_SUCCESS;
}
Compile and run as follows:
gcc -fopenmp -pthread hello.c && ./a.out # Linux
gcc -fopenmp -pthread hello.c && ./a.exe # MSYS2, Windows
As you can see, the only difference between Linux and MSYS2 on Windows
is the name of the executable binary. Everything else is identical.
I tend to think of MSYS2 as an emulated (Arch-)Linux terminal on
Windows.
To install gcc in MSYS2:
yes | pacman -Syu gcc
Expect output similar to:
Hello C code!
In main: creating thread 0
Hello World. - It's me, thread #0
In main: creating thread 1
Hello World. - It's me, thread #1
After join: I am always last. Bye-bye!
Reference
https://www.msys2.org/wiki/MSYS2-installation/

Related

Ncurses No Output

Platform: Linux 3.2.0 x86 (Debian Wheezy)
Compiler: GCC 4.7.2 (Debian 4.7.2-5)
I am writing a program that requires advanced terminal control that is provided by ncurses but I cannot get my program to print anything to stdscr. For example if I compiled the following code I would not see "Testing.. Testing" on the screen. I have used ncurses before and I have never encountered such a problem. I do not know if this is relevant or not but I am running a fresh install of Debian (I literally installed it a couple of hours ago).
#include <ncurses.h>
int main()
{
initscr();
printw("Testing... Testing");
refresh();
return;
}
Also the above progam was compiled with,
gcc --all-warnings --extra-warnings -std=c11 filename.c -lncurses
If you want to see the text, maybe you should keep the program running when you're printing it.
#include <ncurses.h>
int main()
{
initscr();
printw("Testing... Testing");
refresh();
getch(); // Wait the user input in order to keep the program active and showing text.
endwin(); // Terminate the window to clean all memory allocations.
return;
}
You can get more informations on the ncurses "hello world": http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/helloworld.html

Getting OpenMP running in Code::Blocks

I am trying to teach myself OpenMP using Windows 7, but I am having a hard time getting Code::Blocks to compile a basic hello world program:
#include <omp.h>
#include <stdio.h>
int main()
{
#pragma omp parallel
printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
}
I have made some progress, but there is one remaining persistent error that I can't get rid of.
I have -fopenmp in my compiler "Compiler->Compiler Settings->Other Options"
I have -gomp and -pthreads in "Compiler->Linker Settings->Other linker options"
I have C:\Program File (x86)\Codeblocks\MinGW\gcc\mingw32\bin in "Compiler->Toolchain exectuable->Additional Paths"
When I compile, I get the error: "ld.exe: cannot find -lpthread"
Can someone suggest what I might have set up wrong?
Thanks!
The linker complains about a missing library. pthreads is the library that implements the threading interface that your OpenMP implementation uses to do all the threading stuff.
The library is called "libpthread.a" (static version) and "libpthread.so" (dynamic version) on the disk. Try to find these two on the file system under your MinGW directory. They likely reside in a directory called "lib" or "lib64". If either one is missing, then you might need to install an additional package.
Cheers,
-michael

C90 printf with \n or \r\n not working in cygwin; but fflush(stdout); works fine. why?

Cygwin64 bit
Command to compile:
gcc hello.c -o hello -ansi -pedantic-errors
Command to run
./hello
hello.c
#include<stdio.h>
int main() {
/*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
printf("hello world!\n");
printf("hello world again!\r\n");
/*fflush(stdout); without fflush, the above strings are not showing*/
while(1)
{
}
}
Questions:
I don't want a fflush after every printf to let the terminal show the string in time, then how?
ANSWER: setbuf(stdout, 0);
Why is "\n" or "\r\n" not working in my case considering lots of posts pointed out a line break will fix the problem?
Is it true that cygwin's terminal behaves differently than normal Linux's terminal? Since I don't have linux installed, anyone give a test for me?
Or let me ask a more general question: On which kinds of terminals, the sentence "a new line will force flush" is true?
Thanks
It seems that in Cygwin, stdout isn't identified as a terminal (but as a pipe), so it isn't line-buffered by default.
Based on this answer, perhaps linking with the Cygwin DLL would help.
That program works for me under 32-bit Cygwin. Specifically, when I compile and execute this program:
#include<stdio.h>
int main() {
/*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
printf("hello world!\n");
printf("hello world again!\r\n");
/*fflush(stdout); without fflush, the above strings are not showing*/
while(1)
{
}
}
it produces this output:
hello world!
hello world again!
and then hangs until I kill it with Ctrl-C.
I get the same behavior invoking the program from bash under the Windows console, mintty, and xterm (I doubt that the terminal would make any difference).
I'm using 32-bit Cygwin under 64-bit Windows 7. You say you're using 64-bit Cygwin, which was just announced a few days ago; I haven't tried it yet.
I suspect an issue with 64-bit vs. 32-bit Cygwin. I suggest you post to the Cygwin mailing list.
Here's a cleaned-up version of your program that should exhibit the same issue (please verify that the comments are correct):
#include <stdio.h>
int main(void) {
/* Adding setbuf(stdout, 0) here makes the output appear */
printf("hello world!\n");
/* Adding fflush(stdout) here makes the output appear */
while(1) {
/* nothing */
}
}
I know nothing about cgywin. but here I do a test in Linux.
I try the code below, and compile by : gcc -std=c90 filename.c
Without fflush It print all words before loop,so I think the newline flush the buffer! It just work!
I use SUSE gcc .
#include<stdio.h>
int main() {
/*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
printf("hello world!\n"); /*without fflush, not shown*/
printf("hello world again!\r\n"); /*without fflush, not shown*/
/* fflush(stdout);*/
while(1)
{
}
}
I've just had this problem. I've got some code I've been working on a while and had to reinstall Cygwin on my PC. I installed the 64-bit version this time, it was x86 previously.
Built my code, ran it in the bash shell and no output.
What I didn't realize is that I have a Cygwin1.dll from the previous 32-bit version of cygwin still in my bin folder. I renamed this and ran my program and the output worked.
You may have a different issue, but if you've got different dll versions in use your bin other folders, it may cause this problem.

Using a different version of glibc?

I want to do some modifications to the glibc library. The first step is to be able to use a specific version when I compile a program. I'm under ubuntu 12.10 and my directories are :
/mydirectory/glibc-2.17 (where I have extracted the last version from the website)
/mydirectory/glibc-2.17-build (where I have executed the configure and make command)
/mydirectory/test/helloworld.c (where I have my helloworld program)
The helloworld.c is the following:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
char glibc[256] = "xxxx"; /* How to detect the glibc version here ? */
printf("hello, world\n");
printf("glibc version = %s\n", glibc);
return 0;
}
First how can I print the version of glibc ? (I think that there is a macro/constant in glibc for that).
Second, what command line should I use to compile my helloworld.c file to use the glibc that is in /mydirectory/glibc-2.17-build ?
Use -L pathname to explicitly specify a pathname to ld as Barmar has said in the comment.
It's suggested to use static linking -static or there might be problems during execution I think.
Actually my own solution to this problem would be: compile and link the source code as normal, and invoke with LD_PRELOAD set to your specified version of shared objects.
See http://linux.die.net/man/8/ld.so

Cross compile Static Library from Linux for windows

I want to compile static library in linux for windows. Following is the procedure I followed for compiling
Compile the source code of static library in linux using i586-mingw32msvc-cc -c static_lib.c -o static_lib.o
Created the static library in linux ar rv static_lib.a static_lib.o and ranlib static_lib.a
I created a sample program in eclipse on windows and linked this static library which is cross compiled in linux for windows. The compiler used at windows was mingw.
while compiling the program in windows eclipse, the compiler gives me the following error.
static_test\static_lib.a: file format not recognized; treating as linker script
\static_test\static_lib.a:1: syntax error
collect2: ld returned 1 exit status
Build error occurred, build is stopped
The Codes are as follows:
static_lib.c
#include <stdio.h>
void func(void)
{
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
}
sample_static.c
#include <stdio.h>
extern void func(void);
int main ()
{
printf ("Main function\n");
func();
}
kindly give me suggestions to compile and get it work.
Regards
Johnnie Alan
Try using the cross-compiler archiver instead of the native one, i.e. use i586-mingw32msvc-ar and i586-mingw32msvc-ranlib instead of ar and ranlib.
Or is that just a typo on the question?
Try i586-mingw32msvc-ar instead of plain ar. Typically ar in Linux will not support the PE format used for Windows programming. (Or you will have to instruct it to use PE format.)

Resources