简体   繁体   English

C在linux中编程Makefile

[英]C programing Makefile in linux

Can anyone hellp me cuz i am not sure what is wrong i think i am doing something wrong in Makefile cuz cant run it i get error任何人都可以帮助我因为我不确定出了什么问题我想我在 Makefile 中做错了因为无法运行它我得到错误

PROGRAM:drevoProcesov.c`程序:drevoProcesov.c`

#include <sys/types.h>`  
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>`

void izpis()
{
printf("PID: %d  PPID: %d \n", getpid(), getppid());
}

int main(int argc, char *argv[])
{
    /*levo*/
    if(fork()==0)
    {
            if(fork()==0)
            {
                    if(fork()==0)
                    {
                            izpis();
                            exit(0);                       
                    }              
                    else
                    {
                            wait(0);       
                            izpis();                       
                    }
                    exit(0);
            }
            else
            {
                    wait(0);
                    wait(0);
                    wait(0);
                    izpis();
            }
    }      
    /*desno*/
    else
    {
            wait(0);
            if(fork()==0)
            {  
                    if(fork()==0)
                    {
                            izpis();
                            exit(0);
                    }
                    else
                    {
                            wait(0);
                            if(fork()==0)
                            {
                                    izpis();
                                    exit(0);
                            }
                            else
                            {
                                    wait(0);
                                    if(fork()==0)
                                    {
                                            izpis();
                                            exit(0);
                                    }
                                    else
                                    {
                                            wait(0);
                                            izpis();
                                    }
                            }
                    }
                    exit(0);
            }
            else
            {
                    wait(0);
                    izpis();
            }
    }
    return 0;

} }

MAKEFILE制作文件

CC=gcc
LDFLAGS=-lm -g3 -lpthread -Wall
TARGET=drevoProcesov
all: $(TARGET)

clean:
rm -rf $(TARGET)
rm -rf *.o

THIS IS ERROR I GET FOR: drevoProcesov.c这是我得到的错误:drevoProcesov.c

./drevoProcesov.c: line 7: syntax error near unexpected token `('
./drevoProcesov.c: line 7: `void izpis()'

PROGRAM 2 vsotast.c程序 2 vsotast.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int *vsota;

int main(int argc,char *argv[]) {

if (argc != 2) {
  printf("Napaka v podajanju argumentov!\nPrimer: ./vsotast 5\n");
  return -1;
}

int n = atoi(argv[1]);

vsota = (int *)malloc(sizeof(int));
*vsota = 0;

int ret;
for (int i=0; i<n; i++)
{
  ret = fork();
  if (ret== 0) {
     *vsota = (*vsota + (i+1));

     //printf("VSOTA: %d; I+1: %d\n",*vsota, i+1);

     /*if (i == n-1)
        printf("Vsota, ki so jo izračunali sinovi: %d\n", *vsota);*/

     exit(0);
  } else {
     wait(&ret);
  }      
}

int sum = ((n*(1+n))/(2));

printf("Vsota, ki so jo izračunali sinovi: %d\n", *vsota);
printf("Vsota, ki jo izračunal oce: %d\n",sum);

free(vsota);

return 0;
}

MAKEFILE 2制作文件 2

CC = gcc
LDFLAGS = -lm -g3 -lpthread -Wall
CFLAGS = -g
TARGET = vsotast

all: $(TARGET)

clean:
rm -rf $(TARGET)
rm -rf *.o

THIS IS ERROR I GET FOR: vsotast.c这是我得到的错误:vsotast.c

gcc -g  -lm -g3 -lpthread -Wall  vsotast.c   -o vsotast
vsotast.c: In function ‘main’:
vsotast.c:22:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
for (int i=0; i<n; i++)
^
vsotast.c:22:4: note: use option -std=c99 or -std=gnu99 to compile your code

The vsotast.c error is due to the fact that declarations are not allowed in a for loop expression prior to C99, so you either need to enable C99 mode or move the declaration out of the for expression: vsotast.c错误是由于在 C99 之前的for循环表达式中不允许声明,因此您需要启用 C99 模式或将声明移出for表达式:

int i;
for (i = 0; i < n; i++)

Not a complete answer, just (longer) thoughts:不是一个完整的答案,只是(更长的)想法:

  1. indent your Makefile s correctly.正确缩进你的Makefile It's crucial for them.这对他们来说至关重要。
  2. compiler errors have almost nothing w/ Makefile - if the compiler runs then you created a more or less correct Makefile , and the problem is in your code :)编译器错误几乎与 Makefile 无关 - 如果编译器运行,那么您创建了或多或少正确的Makefile ,问题出在您的代码中:)

in this particular case the problematic string is在这种特殊情况下,有问题的字符串是

for (int i=0; i<n; i++)

it's C, not C++, and you can't declare loop variables just inside for statement, unless you pass -std=c99 flag to compiler (use CFLAGS in Makefile for this)它是 C,而不是 C++,并且您不能在 for 语句中声明循环变量,除非您将-std=c99标志传递给编译器(为此在Makefile使用CFLAGS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM