thanks for your reply!
Here is the c source code:
---------------------------------------------
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
char * myFunction ();
int main(int argc, char *argv[])
{
char *str = myFunction();
printf("%s\n", str);
return 0;
}
char * myFunction () {
char *sub_str = malloc(1100 * sizeof(char));
//char* sub_str = new char[100];
char *split = ":";
char *end = ";";
struct ifaddrs *ifaddr, *ifa;
int family, s;
int x=0;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
exit(EXIT_FAILURE);
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
continue;
s=getnameinfo(ifa->ifa_addr,sizeof(struct
sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if(ifa->ifa_addr->sa_family==AF_INET)
{
if (s != 0)
{
printf("getnameinfo() failed: %s\n",
gai_strerror(s));
exit(EXIT_FAILURE);
}
if(x==0){
strcpy(sub_str, ifa->ifa_name);
strcat(sub_str, split);
strcat(sub_str, host);
strcat(sub_str, end);
}else{
strcat(sub_str, ifa->ifa_name);
strcat(sub_str, split);
strcat(sub_str, host);
strcat(sub_str, end);
}
x++;
}
}
int len = strlen(sub_str);
sub_str[len - 1] = '\0';
freeifaddrs(ifaddr);
return sub_str;
}
---------------------------------------------------
and the command-line flags is gcc -o getip getip.c
On Thursday, May 10, 2018 at 12:00:00 AM UTC+8, Brion Vibber wrote:
>
> On Wed, May 9, 2018, 6:28 AM zhatian diao <[email protected]
> <javascript:>> wrote:
>
>> Hello everyone , I m new to emcc.
>>
>> Today I followed the user guide to install emcc and run the hello
>> world example successfully.
>>
>
> Welcome! :)
>
>
>> However, things went wrong when I tried to compile another c file to
>> js file.
>>
>> At first, I got an error
>>
>> testfiles/getip.c:10:17: fatal error: 'linux/if_link.h' file not
>> found
>> #include <linux/if_link.h>
>> ^~~~~~~~~~~~~~~~~
>> 1 error generated.
>> ERROR:root:compiler frontend failed to generate LLVM bitcode,
>> halting
>>
>> The strange thing is that I compiled the c file early with gcc
>> without error. It can be run successfully.
>>
>
> emscripten is not Linux, and does not include Linux-specific kernel
> interfaces. Think of it as cross-compiling to another operating system...
> If your source code needs linux/if_link.h for Linux-specific code, it will
> need to be modified to build for emscripten.
>
> Note that the networking support in emscripten is limited because the web
> runtime limited what is possible; you can't look up IP addresses or connect
> to arbitrary hosts or ports. Make sure your test program is something that
> would actually work.
>
>
>> So I changed my code and removed "<linux/if_link.h>". I got another
>> compile warning: warning:unresolved symbol: htons.
>>
>
> It s source c code can also be compiled with gcc.
>>
>> Is there anything I missed or unconfigured?
>>
>>
>>
>>
>> Compile enviroment : ubuntu
>>
>> head include:
>>
>> #include <arpa/inet.h>
>> #include <sys/socket.h>
>> #include <netdb.h>
>> #include <ifaddrs.h>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <unistd.h>
>> #include <string.h>
>> #include <linux/if_link.h>
>>
>
> htons is defined in arpa/inet.h which you're also including, so it should
> work. Can you provide a minimal test case? (Small complete .c file that can
> be compiled, and the command-line flags you're using to compile it)
>
> -- brion
>
--
You received this message because you are subscribed to the Google Groups
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.