【问题标题】:How(exactly) do I change the GCC directory search path?如何(确切地)更改 GCC 目录搜索路径?
【发布时间】:2017-08-27 19:49:06
【问题描述】:

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

#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");
         }

       }

  }

}

当我输入命令“gcc sctp_ec.c”时,我得到以下输出:

-*- 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 代码文件所在的第一个路径中搜索only

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

【问题讨论】:

    标签: c linux gcc emacs24


    【解决方案1】:

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

    【讨论】:

    • 如何在我的 .profile 或 .login 文件中设置它?这听起来像我正在寻找的。​​span>
    • 您只需使用文本编辑器(vi 或 emacs 是 Unix 系统上的典型编辑器)。您可能使用 bash 作为 shell,因此您想添加行 export C_INCLUDE_PATH="/path/to/dir1:/path/to/dir2:/another/dir" 然后运行命令 % . ~/.profile(是的,这是一个 '.')让它在您当前的 shell 中生效。它会在你以后启动的所有 shell 中自动发生
    【解决方案2】:

    使用这些命令行选项之一:

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

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

    【讨论】:

    • 两个选项:makefile 或为您创建它的东西。我个人更喜欢Eclipse。我不使用 emacs,所以不能提供任何建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2012-09-03
    相关资源
    最近更新 更多