SavedbyZer0 SavedbyZer0 - 2 months ago 16
C Question

How(exactly) do I change the GCC directory search path?

OK so here is the code I am trying to compile:

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

}

}


}

And when I enter the command "gcc sctp_ec.c" I get the following output:

-*- 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


So I know what the problem is. The GCC compiler is searching in the wrong directory! I want it to search for files in the path: /usr/lib/gcc/x86_64-linux-gnu/4.8.4/include instead of /usr/include. I tried using the -I, -Idir, and -isystem commands but to no avail. With the -isystem it searches in both of those paths and there are definition conflicts. So what do I do here to permanently change the GCC/emacs24 program to search only in the first path where the .c code file is?

FTR I am running emacs24 and GCC on Linux Mint 18.1.

Answer Source

If you set the environment variable C_INCLUDE_PATH to the list of directories you want on the search path, it will be permanent (set it in your .profile or .login file, depending on your shell). The value should be a colon-separated list of directories you want searched.