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

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

       }

  }

当我输入命令“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 代码文件所在的第一个路径中搜索?

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

最佳答案

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

关于c - 如何(确切地)更改 GCC 目录搜索路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45908734/

相关文章:

c - 如何更改键盘布局(X11 API 解决方案)

c++ - 返回 NULL 指针,从 'long int' 到 'Object*' 的无效转换

c - 具体来说,转换 malloc 的结果有什么危险?

linux - 用于端口路由的 docker ha 代理

c - 指向视频内存 (0xB8000) 的指针有些奇怪

linux - 为什么我的 crontab 不工作?

c++ - 在 64 位字上进行 32 位比较和交换

powershell - 如何在 Windows 命令行上获取 MinGW64、C/C++ 语言关键字和库函数的帮助?

c - C 中的字符串减速长度

java - 使用 JNI 将 C++ 类成员函数绑定(bind)到 Java