我如何(确切地)更改GCC目录搜索路径?

时间:2017-08-27 19:49:06

标签: c linux gcc emacs24

好的,这就是我要编译的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <net/sctp.h>


#define MAX_BUFFER 1024

void die(char *s)
{
      perror(s);
      exit(1);
}


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

  int    connector,flags,r;
  u_int  port;
  struct hostent*        host;
  struct in_addr         in;
  struct sockaddr_in     rmaddr;
  struct sctp_initmsg    initmsg;
  struct sctp_sndrcvinfo sinfo;
  struct sctp_event_subscribe events; 
  bool   connected = false;   
  char   buffer[MAX_BUFFER];
  char*  exit = "quit";

  if(argc!=2) {
        printf("Usage: %s ipaddress\n", argv[0]);
    return -1;
  }



  if((connector = socket(AF_INET,SOCK_STREAM, IPPROTO_SCTP))<0){
      perror("socket");
      return -1;
   }



  printf("Enter the port number you wish to connect(on): ");
  scanf("%u", &port);
  printf("\n");

  if(port==0){
              printf("ERR0R: Port number must be between 1 & 65,535\n");
              printf("\n");
              printf("Enter the port number you wish to connect(on): ");
              scanf("%u", &port);
              printf("\n");        
  }


  memset( &initmsg, 0, sizeof(initmsg) );
  initmsg.sinit_num_ostreams = 3;
  initmsg.sinit_max_instreams = 3;
  initmsg.sinit_max_attempts = 2;
  if(setsockopt(connector,IPPROTO_SCTP,SCTP_INITMSG,&initmsg,sizeof(initmsg))<0){
       perror("setsockopt");
       return -1;
  }

  bzero( (void *)&rmaddr, sizeof(rmaddr) );
  rmaddr.sin_family = AF_INET;
  inet_pton(AF_INET, argv[1], &rmaddr.sin_addr);
  rmaddr.sin_port = htons(port);



  connect(connector,(struct sockaddr*)&rmaddr,sizeof(rmaddr));

       connected=true;
       memset( (void *)&events, 0, sizeof(events) );
       events.sctp_data_io_event = 1;
       setsockopt(connector, SOL_SCTP, SCTP_EVENTS,(const void *)&events, sizeof(events));
       printf("\n");
       printf("Connected to host: %s",argv[1],"on port %u",port);
       printf("     type 'quit' to disconnect\n");
       printf("\n");



  while(connected==true){

       int nbs;
       int nbr = 0;
       int flags = MSG_NOSIGNAL;
       printf(">");
       scanf("%s",buffer);
       printf("\n");

       sinfo.sinfo_flags = flags;

       nbs = send(connector,(void*)&buffer,sizeof(buffer),flags);        



       printf("\n");
       printf("# bytes sent %i\n", nbs);
       printf("\n");

       if(nbs<0){
        perror("sendmsg");
            close(connector);
            return -1;
       }

       while(nbr < nbs){
         socklen_t len = sizeof(rmaddr);
         nbr = recv(connector,(void*)&buffer,sizeof(buffer),flags);


         if(nbr<0){
               perror("recvmsg");
               close(connector);
               return -1;
         }else{
           printf(">>");
               printf("%s\n",buffer);
               printf("\n");
         }

       }

  }

}

当我输入命令&#34; gcc sctp_ec.c&#34;我得到以下输出:

-*- mode: compilation; default-directory: "/usr/lib/gcc/x86_64-linux-gnu/4.8.4/include/" -*-
Compilation started at Sun Aug 27 12:43:50

gcc sctp_ec.c
In file included from sctp_ec.c:10:0:
/usr/include/net/sctp.h:55:22: fatal error: net/ipv6.h: No such file or directory
compilation terminated.

Compilation exited abnormally with code 1 at Sun Aug 27 12:43:50

所以我知道问题是什么。 GCC编译器正在错误的目录中搜索!我希望它在路径中搜索文件:/usr/lib/gcc/x86_64-linux-gnu/4.8.4/include而不是/ usr / include。我尝试使用-I,-Idir和-isystem命令,但无济于事。使用-isystem,它会在这两个路径中进行搜索,并且存在定义冲突。那么我该怎么做才能永久更改GCC / emacs24程序,以便在.c代码文件所在的第一个路径中搜索

FTR我在Linux Mint 18.1上运行emacs24和GCC。

2 个答案:

答案 0 :(得分:1)

使用以下命令行选项之一:

> -I dir
> -iquote dir
> -isystem dir
> -idirafter dir

如果确实需要更改系统目录的搜索顺序,请使用-nostdinc和/或-isystem选项。

答案 1 :(得分:0)

如果将环境变量C_INCLUDE_PATH设置为搜索路径上所需的目录列表,则它将是永久性的(将其设置在.profile或.login文件中,具体取决于您的shell)。该值应该是要搜索的以冒号分隔的目录列表。