Is there anything i can do to fix this issue - gcc

I am new to coding and I saved my firstprog.c in a file <C:\Users\chinm\Desktop\S\Codes Book>
my code in firstprog goes as follows
#include <stdio.h>
int main()
{
int i;
for(i=0; i<10; i++)
{
printf("Hello!\n");
}
return 0;
}
But when i type this in cmd i dont get a desired output
OUTPUT:
C:\Users\chinm\Desktop\S\Codes Book>gcc firstprog.c
C:\Users\chinm\Desktop\S\Codes Book>

With the gcc firstprog.c, you only compile the program. You might see that after running gcc firstprog.c, you'll get a file called a.out.
You need to run that file with:
./a.out
See https://web.stanford.edu/class/archive/cs/cs107/cs107.1212/resources/gcc for more details

Related

I am trying to write a shell program to execute more than one command at a time

My Code
#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
char * arg_list[3];
arg_list[0] = "ls";
arg_list[1] = "-l";
arg_list[2] = 0;
char *arg_list2[3];
arg_list2[0] = " ps";
arg_list2[1] = "-ef";
arg_list2[2] = 0;
for(int i=0;i<5;i++){ // loop will run n times (n=5)
if(fork() == 0) {
if (i == 0){
execvp("ls", arg_list);
}else if(i==1){
execvp("ps" , arg_list2);
}else if(i>1){
printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid());
exit(0);
}
}
}
for(int i=0;i<5;i++) // loop will run n times (n=5)
wait(NULL);
}
ME trying to modify it
#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
for(int i=0;i<5;i++){ // loop will run n times (n=5)
if(fork() == 0) {
printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid());
execlp(argv[i],argv[i],argv[i+1],(char*)NULL);
exit(0);
}
}
for(int i=0;i<5;i++) // loop will run n times (n=5)
wait(NULL);
}
-- NEED GUIDANCE AND UNDERSTANDING
I am trying to make my own tiny little shell program. When I run my first code works fine, runs all commands on the command line. But I cannot know and define all commands the user might enter. So i am trying to get a base code which could run any commands single or multiple entered by user. I tried using execlp where it does not compile saying argv is not defined which is true as i don't want to specifically define it.
I am trying to make my own tiny little shell program. When I run my first code works fine, runs all commands on the command line. But I cannot know and define all commands the user might enter.
For sure.... A shell program purpose is basically:
Read user input
Execute user input
Return result of execution.
There's nothing in your code that read user input....
So i am trying to get a base code which could run any commands single or multiple entered by user.
So read user input ;-)
I tried using execlp where it does not compile saying argv is not defined which is true as i don't want to specifically define it.
For sure ... but how would GCC guessed that `argv[]̀ must be automaticallty filled with user input ?
There's nothing automatic when coding in C language. You have to manage this manually.
Also, note that argc, argv et envp are usually reserved for main() function:
main(int argc, char **argv, char **envp)
So you may use something else to build your command array.
In pseudo code, what you must implement is:
quit=0
while (quit = 0) {
command_to_run = read_user_input();
if (command_to_run == "exit") {
quit = 1;
} else {
execute(command_to_run);
}
}
Some advices:
Try to use more functions. For example, implement a fork_and_run(char **cmd) function to fork and then execute command provided by the user. Il will make your code more readable and easy to maintain.
Read carefully manpages: everything you should know (like, for example, the fact that array provided to execvp() must be NULL-terminated) is written in it.
Your debugging messages should be printed to stderr. The result of the command run must be printed to stdin, so use fprintf() instead of printf() to write to the correct stream.
I would use a #define debug(x) fprintf(stderr, x) or something similar for debugging output so that you can easily disable later ;-)

warning #3180: unrecognized OpenMP #pragma

I am having a really hard time in implementing openMP code on my mac machine on Terminal with icc compiler. I find the following error! Please do help me with the correction of error.
The following code is pasted as follows. IT NEVER WORK FOR openMP for, reduce either. The pragma is just not recognising. Appreciate yourself trying the code to help.
#include <stdio.h>
#include <omp.h>
int main()
{
#pragma omp parallel for
{
for(int i=0;i<3;i++)
{
printf("Hello");
}
}
return 0;
}
To add to my comment, the correct version of the code is
#include <stdio.h>
#include <omp.h>
int main()
{
#pragma omp parallel for
for(int i=0;i<3;i++)
{
printf("Hello");
}
return 0;
}
The proper compiler command line is icc -fopenmp ... -o bla.exe bla.c (assuming that the file is named bla.c). Please replace ... with the other command line options that you will need for your code to compile.
UPDATE: The proper compiler command line for the new OpenMP compilers from Intel is to use -fiopenmp (needs -fopenmp-targets=spir64 for GPUs).

Weird C library linkage issues on Mac - Segmentation Fault

I have a strange segmentation fault that doesn't exist when everything is in 1 .c file, but does exist when I put part of the code in a dynamically linked library and link it to a test file. The complete code for the working 1 .c file code is at the bottom, the complete code for the error system with 2 .c and 1 .h file come first.
Here is the error system:
example.h:
#include <stdio.h>
#include <stdlib.h>
typedef struct MYARRAY {
int len;
void* items[];
} MYARRAY;
MYARRAY *collection;
void
mypush(void* p);
example.c:
#include "example.h"
void
mypush(void* p) {
printf("Here %lu\n", sizeof collection);
puts("FOO");
int len = collection->len++;
puts("BAR");
collection->items[len] = p;
}
example2.c:
This is essentially a test file:
#include "example.h"
void
test_print() {
puts("Here1");
mypush("foo");
puts("Here2");
}
int
main() {
collection = malloc(sizeof *collection + (sizeof collection->items[0] * 1000));
collection->len = 0;
puts("Start");
test_print();
puts("Done");
return 0;
}
Makefile:
I link example to example2 here, and run:
example:
#clang -I . -dynamiclib \
-undefined dynamic_lookup \
-o example.dylib example.c
#clang example2.c example.dylib -o example2.o
#./example2.o
.PHONY: example
The output is:
$ make example
Start
Here1
Here 8
FOO
make: *** [example] Segmentation fault: 11
But it should show the full output of:
$ make example
Start
Here1
Here 8
FOO
BAR
Here2
Done
The weird thing is everything works if it is this system:
example.c:
#include <stdio.h>
#include <stdlib.h>
typedef struct MYARRAY {
int len;
void* items[];
} MYARRAY;
MYARRAY *collection;
void
mypush(void* p) {
printf("Here %lu\n", sizeof collection);
puts("FOO");
int len = collection->len++;
puts("BAR");
collection->items[len] = p;
}
void
test_print() {
puts("Here1");
mypush("foo");
puts("Here");
}
int
main() {
collection = malloc(sizeof *collection + (sizeof collection->items[0] * 1000));
collection->len = 0;
puts("ASF");
test_print();
return 0;
}
Makefile:
example:
#clang -o example example.c
#./example
.PHONY: example
Wondering why it's creating a segmentation fault when it is linked like this, and what I am doing wrong.
I have checked otool and with DYLD_PRINT_LIBRARIES=YES and it shows it is importing the dynamically linked libraries, but for some reason it's segmentation faulting when linked but works fine when it isn't linked.
Your problem is this, in example.h:
MYARRAY *collection;
Since both main.c and example.c include this file, you end up defining collection twice, which results in undefined behavior. You need to make sure you define each object only once. The details are relatively unimportant since anything can happen with undefined behavior, but what's probably happening is that main.c is allocating memory for one object, but the one example.c is using is still NULL. As mentioned in the comments, since you define collection in main.c your linker is able to build the executable without needing to look for that symbol in the dynamic library, so you don't get a link time warning about it being defined there too, and obviously there'd be no cause for a warning at the time you compile the library.
It works for you when you put everything in one file because obviously then you're not defining anything twice, anymore. The error itself is nothing to do with the fact you're using a dynamic library, although that may have made it harder to detect.
It would be better to define this in example.c and provide a constructor function, there's no need for main() to be able to access it directly. But if you must do this, then define it in example.c and just declare an extern identifier in the header file to tell main.c that the object is defined somewhere else.

detect output stream on linux shell script

I'm trying write a shell scrip on linux to detect a string on output steam.
This is my shell script
#!/bin/bash
./binary
binary file is compiled from source file as below:
gcc-4.6 main.c -o binary
//main.c
#include "stdio.h"
void main(){
int i;
for (i=0; i<100; i++){
printf("data: %d\n", i);
sleep(1); // delay 1s
}
}
Could you let me know how to detect "data: 10" from output ./binary?
When stdout is not connected to a terminal, it's fully buffered by default. So if you want to be able to detect output immediately (as suggested by the sleep(1); in the code) you need to flush the buffer after printing.
#include "stdio.h"
void main(){
int i;
for (i=0; i<100; i++){
printf("data: %d\n", i);
fflush(stdout);
sleep(1); // delay 1s
}
}
Then you can pipe the output of the program to something in the script and it will detect the output without waiting for the program to finish.

Could not compile when using FreeBSD-based tcphdr on Centos

I am writing a simple program that using struct tcphdr in netinet/tcp.h as follows:
#define _BSD_SOURCE
#include <netinet/tcp.h>
#include <stdio.h>
int main()
{
struct tcphdr t;
t.th_sport = 0;
printf("\n%d", t.th_sport);
return 1;
}
Because I want this program can work on both FreeBSD and Centos, so I am using the FreeBSD-based property. I also defined _BSD_SOURCE in the beginning of file. But it could not be compiled using std=c++11 when I save this source code into *.cpp file. There is no member named th_sport. However, it is compiled perfectly by std=c99 with *.c file.
What's problem here? Anyone helps me to explain this? Thanks so much.

Resources