gcc -pthread no such fiile or directory ex_thread_creation - c

I'm trying to print infinite lines "Hello, how are you", "I'm fine, and you"
first I use command "vim ex_thread_creation"
then I enter following the code:
#include <pthread.h>
#include <stdio.h>
void *thread_print(void * messenge)
{
while(1) {
printf("Hello, How are you?\n");
}
}
int main()
{
pthread_t idthread;
pthread_create( &idthread,NULL, &thread_print, NULL);
while(1) {
printf("Iā€™m fine, and you?\n");
}
return 0;
}
then I use gcc ex_thread_creation.c -pthread ex_thread_creation
then I meet the error: no such file or directory ex_thread_creation.
someone help me, please
edit 1: after I change -pthread to -o I meet another error: ex_thread_creation.c:(.text+0x4a): undefined reference to pthread_create collect2: error: old returned 1 exit status

There are several problems with your approach.
Regarding your gcc issue, the correct command would be:
gcc ex_thread_creation.c -o ex_thread_creation -lpthread
The -o flag stands for outfile or the file resulted from compiling the source file.
Regarding your code there are several issues too other than your code indentation.
The lines "Hello, how are you" and "I'm fine, and you" might not appear one after the other as intended. The output may appear malformed (the lines being intertwined) or the order could be wrong (Hello -> Hello -> I'm fine -> I'm fine).
If you want to keep the output as intended in your question, I suggest you use synchronization mechanisms.

after research a bit longer, i've found the answer,
the correct command is: gcc -pthread -o term term.c
thank you for reading

Related

Unable to read from a file in C despite getting a success from fopen()

Thanks , this has been solved , please see updated code and output in the answer . Thanks Jonathan and everyone else.
I have written below code to read the file present in same directory.
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
int main(){
FILE *fptr;
/*Tried putting different combinations like filename with
quotes|filename without quotes|complete path with quotes|complete path
without quotes*/
if((fptr=fopen("TestFile.txt","r"))==NULL){
printf("\nfopen() returning NULL: %d , %s \n",errno,strerror(errno));
}else{
printf("\nfopen() returning something else: %d , %s
\n",errno,strerror(errno));
}
int c;
while((c=fgetc(fptr))!=EOF){
printf("%c",c);
}}
And i am was getting below output :
./a.out
Segmentation fault (core dumped)
And a GDB core analysis had the following :
(gdb) run
Starting program: /home/astitva/Documents/Coding/a.out
Dwarf Error: wrong version in compilation unit header (is 0, should be 2,
3, or 4) [in module /usr/lib/debug/.build-
id/12/5dab90a4cfa8edc5d532f583e08e810c232cd5.debug]
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
Dwarf Error: wrong version in compilation unit header (is 0, should be 2,
3, or 4) [in module /usr/lib/debug/.build-
id/c0/5201cc642f6b800835e811d7cb28f103aeb191.debug]
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7abc496 in strlen () from /lib/x86_64-linux-gnu/libc.so.6
and my text file TestFile.txt was :
DATA ENETERD AT RUN INSTANCE 1 ------> BLABLABLA
DATA ENETERD AT RUN INSTANCE 2 ------> YADAYADAYADA
DATA ENETERD AT RUN INSTANCE 3 ------> FOOBARFOOBAR
To avoid the warning, you need to #include <string.h> in your code. Add an exit(1) in the error-handling if block :
if((fptr=fopen("TestFile.txt","r"))==NULL){
printf("\nfopen() returning NULL: %d %s\n",errno, strerror(errno));
exit(1);
}
The program needs to exit "gracefully" if the file doesn't exist. So, if there is no valid file present, the program will simply exit and not print anything on the stdout.
EDIT : Just adding on Jonathan's helpful comment on ignoring compiler's warnings :
"If you ignored a compiler warning ā€” don't. If the compiler didn't warn you about the undeclared function strerror(), you need to find the options that make it report such problems (if you use gcc, you would use gcc -Wall -Wextra -Werror ā€” and I'd add -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wold-style-declaration too, though clang doesn't like -Wold-style-declaration)."

Cannot compile multi-threading example in C

Having a Major issue with trying to do a multi-threading project for school
I'm just trying to run sample code to see how pthreads work
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int sum;
void *runner(void *param);
int main(int argc, char *argv[]) {
pthread_t tid;
pthread_attr_t attr;
if (argc !=2){
fprintf(stderr, "usage: a.out <interger value>\n");
return -1;
}
if (atoi(argv[1]) < 0) {
fprintf(stderr, "%d must be >= 0\n",atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);//get default attributes
pthread_create(&tid,&attr,runner,argv[1]);//create the thread
pthread_join(tid,NULL);//wait for the thread to exit
printf("sum = %d\n",sum);
}
void *runner(void *param) {
int i, upper = atoi(param);
sum = 0;
for (i =1; i <= upper; i++) {
sum += i;
}
pthread_exit(0);
}
And no matter what I do I can't get the code to compile. I get the following error:
Running "/home/ubuntu/workspace/primefactor/assn3.c"
/tmp/cc58AE5c.o: In function 'main':
assn3.c:(.text+0xc4): undefined reference to 'pthread_create'
assn3.c:(.text+0xd5): undefined reference to 'pthread_join'
collect2: error: ld returned 1 exit status
Process exited with code: 1
I've tried writing my own makefile with the -pthread flag; didn't work....
I've tried running
sudo apt-get install libpthread-stubs0-dev
to install the right library; didn't work...
I'm at my wits end to try and get pthreads_create() and pthreads_join() to work and they're the main requirement for my school project. I've already gotten the project working with a normal C program (take numbers from the command line, get the prime factors for each arg, store them in an array, then pass that array and display the results. ex: ./prime.c.o {1..100} would be run from BASH and it outs 1:1 2: 2 3: 3 4: 2 2 etc...
so that's what the output is supposed to be and I'm supposed to do that with threading, but I can't do that if I can't even compile sample code to see how threads work.
I really have already spent several hours searching, trying, but no luck getting rid of these undefined reference errors for pthread_create() and pthread_join() . Any help would be greatly appreciated in figuring out why I get the above errors.
Edit:
Thanks to those that tried answering for me. I don't know why I got the down vote; I'm not very active here and I do try to be polite when asking for help. Anyways I finally figured it out.
If I am using the make file it had to look like this:
assn3:
gcc -g -Wall -pthread assn3.c -o assn3
Then from the command line: make assn3
It appears you haven't specified to the linker about thread library. For multithreading applications, the usual option for gcc is follows (assume your program file name thread1.c):
gcc -D_REENTRANT -lpthread thread1.c -o thread1
It's also recommended to use -Wall -O3 -pedantic-errors to be pedantic about compiler warnings etc. So this should do:
gcc -Wall -O3 -pedantic-errors -D_REENTRANT -lpthread thread1.c -o thread1

compiling and running a c program using exec()

I am writing a program using execv() that compiles and runs another program. I've written up a simple C program named helloWorld.c that when executed outputs, "Hello world," and a second file named testExec.c that is supposed to compile and run helloWorld.c. I've been looking around everywhere to find a way to do this, but I haven't found any answers. The code in testExec.c is:
#include <stdio.h>
#include <unistd.h>
int main(){
char *args[] = {"./hellWorld.c", "./a.out", NULL};
execv("usr/bin/cc", args);
return 0;
}
testExec.c compiles with no errors. However, when I run it I get an error that says, "fatal error: -fuse-linker-plugin, but liblto_plugin.so not found. compilation terminated." Which I think means helloWorld.c is being compiled but when it comes time to run helloWorld.c this error is thrown. I thought maybe that was because I had a.out and helloWorld.c prefaced with './'. I removed './' from both, then either one individually, and still no luck.
I also did 'sudo apt-get install build-essential' along with 'sudo apt-get install gcc'. I wasn't sure if that would resolve the issue but I really wasn't sure what else to try. Anyway, any help would be appreciated!
You're missing the leading slash when calling cc.
Also, the first argument in the argument list is the name of the executable. The actual arguments come after that. You're also not using -o to specify the name of the output file.
#include <stdio.h>
#include <unistd.h>
int main(){
char *args[] = {"cc", "-o", "./a.out", "./hellWorld.c", NULL};
execv("/usr/bin/cc", args);
return 0;
}
EDIT:
The above only compiles. If you want to compile and run, you can do this:
#include <stdio.h>
#include <unistd.h>
int main(){
system("cc -o ./a.out ./hellWorld.c");
execl("./a.out", "a.out", NULL);
return 0;
}
Although this is probably best done as a shell script:
#!/bin/sh
cc -o ./a.out ./hellWorld.c
./a.out

Why do I get a syntax error when I try to cross compile libperl for mips64?

I'm trying to cross compile net-snmp for mips64, and in order to do that I need the libperl library. I tried to configure libperl for mips64 using the following command:
./Configure -Dcc=/home/toby/x-tools/mips64-n64-linux-gnu/bin/mips64-n64-linux-gnu-gcc -Dprefix=/home/toby/perl
But I got the following error:
Checking your choice of C compiler and flags for coherency...
I've tried to compile and run the following simple program:
#include <stdio.h>
int main() { printf("Ok\n"); return(0); }
I used the command:
/home/toby/x-tools/mips64-n64-linux-gnu/bin/mips64-n64-linux-gnu-gcc -o try -O -I/usr/local/include try.c
./try
and I got the following output:
./try: 1: Syntax error: "(" unexpected
The program compiled OK, but exited with status 2.
(The supplied flags or libraries might be incorrect.)
You have a problem. Shall I abort Configure [y]
How can I fix this?
I'd turn:
#include <stdio.h>
int main() { printf("Ok\n"); return(0); }
Into:
#include <stdio.h>
int main() {
printf("Ok\n");
return(0);
}
And then run the compile command by hand to see which line really contains the syntax error.
That looks like an error from your shell and not the compiler. Particularly because gcc doesn't return "status 2" for a syntax error, but bash does. The problem happens because you have cross compiled a program called ./try for mips64. How do you expect ./Configure to execute it on your host pc? ā€“ indiv

C - Badly Placed ()'s?

So I've been trying to get this code to compile using a gcc compiler using c (I found lots of references to c++ but none to c so I asked this) I kept on getting the error Badly placed ()'s every time I go to run the program. So I simplified it to a very simple Hello World test program and I still get the same error.
What could be causing this error?
#include <stdio.h>
int main(int argc, int* argv[])
{
printf("Hello World\n");
return 0;
}
It seems that you are not trying to execute the compiled binary, but that you have a system that runs a tcsh and you are feeding the C source code directly into that shell:
> tcsh /tmp/badly.c
Badly placed ()'s.
A C program must first be compiled to a binary (here: /tmp/badly), and then you have to execute that binary:
> gcc /tmp/badly.c -Wall -o /tmp/badly
/tmp/badly.c:3:5: warning: second argument of 'main' should be 'char **' [-Wmain]
> /tmp/badly
Hello World
As ouah already noticed in his answer, with the -Wall argument to gcc you also get the informative message that the parameters of your main function are wrong.

Resources