Compiling C++ for Android: select command

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Compiling C++ for Android: select command

Post by bob »

wgarvin wrote:My guess is that "select" is a socket function, and I guess Ares is trying to use it to detect if there is input available to be read.

But the uCLinux platform that compiler is for has been stripped down, and perhaps it does not include that function.

[Edit: the things removed from uCLinux are mostly things that can't be implemented without an MMU. Such as fork(). Sockets could probably be supported, but I don't know if it does.]
There is another way to check for input. I don't remember the specifics, but there is an offset from the 0 descriptor entry that can be tested. I'll see if I can poke thru the Linux source to locate it. I never used it because select() has worked on every unix platform I have tried...
User avatar
Jim Ablett
Posts: 1383
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: Compiling C++ for Android: select command

Post by Jim Ablett »

CRoberson wrote:
wgarvin wrote:My guess is that "select" is a socket function, and I guess Ares is trying to use it to detect if there is input available to be read.

But the uCLinux platform that compiler is for has been stripped down, and perhaps it does not include that function.

[Edit: the things removed from uCLinux are mostly things that can't be implemented without an MMU. Such as fork(). Sockets could probably be supported, but I don't know if it does.]
It is testing for input on stdin.

If this can't be accomplished then that explains all the missing functionality in Android based Chess programs compared to the PC Chess programs.

Hi Charles,

I compiled Garbochess successfully for Android using the Codesourcery toolchain on Windows. Here is the code where Garbochess checks for input (and uses select).
Did you miss including <stdlib.h> ?

Code: Select all


#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>

#include "garbochess.h"

/* Non-windows version */
bool CheckForPendingInput&#40;)
&#123;
  fd_set readfds;
  struct timeval timeout;
   
  FD_ZERO&#40;&readfds&#41;;
  FD_SET&#40;fileno&#40;stdin&#41;, &readfds&#41;;
   /* Set to timeout immediately */
  timeout.tv_sec = 0;
  timeout.tv_usec = 0;
  select&#40;16, &readfds, 0, 0, &timeout&#41;;
   
  return &#40;FD_ISSET&#40;fileno&#40;stdin&#41;, &readfds&#41;);
&#125;
Jim.
User avatar
abik
Posts: 819
Joined: Fri Dec 01, 2006 10:46 pm
Location: Mountain View, CA, USA
Full name: Aart Bik

Re: Compiling C++ for Android: select command

Post by abik »

The NDK-based Android compiler has no issues with the select method either. Is the source of Ares available?
CRoberson
Posts: 2055
Joined: Mon Mar 13, 2006 2:31 am
Location: North Carolina, USA

Re: Compiling C++ for Android: select command

Post by CRoberson »

Hi Jim,

I tried this example. It did not compile with the C++ compiler (it stated select not declared in this scope). When I changed test.cpp to test.c, it did compile with the C compiler. However, it did not link. The link error was can not find -lpthread and can not find -ldl. I took out lpthread and it still said can not find -ldl.

According to the getting started manual, you have to use a -T option to get it to link using some scripts. You didn't show that as needed in the "Dirty on Android" thread.

Here is the command line and the example.

arm-none-eabi-cc.exe -o test test.c -fomit-frame-pointer
-fexceptions -fstrict-aliasing -march=armv5te -O3
-Wno-deprecated -pipe -funroll-all-loops
-ffast-math -static -s -Wl,--whole-archive
-Wl,--no-whole-archive -lc -lm -ldl


Code: Select all

#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h> 

int
main&#40;void&#41; &#123;
    fd_set rfds;
    struct timeval tv;
    int retval;

   /* Watch stdin &#40;fd 0&#41; to see when it has input. */
    FD_ZERO&#40;&rfds&#41;;
    FD_SET&#40;0, &rfds&#41;;

   /* Wait up to five seconds. */
    tv.tv_sec = 5;
    tv.tv_usec = 0;

   retval = select&#40;1, &rfds, NULL, NULL, &tv&#41;;
    /* Don't rely on the value of tv now! */

   if &#40;retval == -1&#41;
        perror&#40;"select&#40;)");
    else if &#40;retval&#41;
        printf&#40;"Data is available now.\n");
        /* FD_ISSET&#40;0, &rfds&#41; will be true. */
    else
        printf&#40;"No data within five seconds.\n");

   return 0;
&#125;

Michel
Posts: 2272
Joined: Mon Sep 29, 2008 1:50 am

Re: Compiling C++ for Android: select command

Post by Michel »

arm-none-eabi-cc.exe
It seems you are using the bare metal toolchain. This means you have to implement everything yourself.

Normally people use the arm-linux toolchain.
User avatar
trojanfoe
Posts: 65
Joined: Sun Jul 31, 2011 11:57 am
Location: Waterlooville, Hampshire, UK

Re: Compiling C++ for Android: select command

Post by trojanfoe »

bob wrote:
wgarvin wrote:My guess is that "select" is a socket function, and I guess Ares is trying to use it to detect if there is input available to be read.

But the uCLinux platform that compiler is for has been stripped down, and perhaps it does not include that function.

[Edit: the things removed from uCLinux are mostly things that can't be implemented without an MMU. Such as fork(). Sockets could probably be supported, but I don't know if it does.]
There is another way to check for input. I don't remember the specifics, but there is an offset from the 0 descriptor entry that can be tested. I'll see if I can poke thru the Linux source to locate it. I never used it because select() has worked on every unix platform I have tried...
poll()? It's basically a better select() but uses an array of file-descriptors and input/output event descriptors.
User avatar
Jim Ablett
Posts: 1383
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: Compiling C++ for Android: select command

Post by Jim Ablett »

CRoberson wrote:Hi Jim,

I tried this example. It did not compile with the C++ compiler (it stated select not declared in this scope). When I changed test.cpp to test.c, it did compile with the C compiler. However, it did not link. The link error was can not find -lpthread and can not find -ldl. I took out lpthread and it still said can not find -ldl.

According to the getting started manual, you have to use a -T option to get it to link using some scripts. You didn't show that as needed in the "Dirty on Android" thread.

Here is the command line and the example.

arm-none-eabi-cc.exe -o test test.c -fomit-frame-pointer
-fexceptions -fstrict-aliasing -march=armv5te -O3
-Wno-deprecated -pipe -funroll-all-loops
-ffast-math -static -s -Wl,--whole-archive
-Wl,--no-whole-archive -lc -lm -ldl


Code: Select all

#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h> 

int
main&#40;void&#41; &#123;
    fd_set rfds;
    struct timeval tv;
    int retval;

   /* Watch stdin &#40;fd 0&#41; to see when it has input. */
    FD_ZERO&#40;&rfds&#41;;
    FD_SET&#40;0, &rfds&#41;;

   /* Wait up to five seconds. */
    tv.tv_sec = 5;
    tv.tv_usec = 0;

   retval = select&#40;1, &rfds, NULL, NULL, &tv&#41;;
    /* Don't rely on the value of tv now! */

   if &#40;retval == -1&#41;
        perror&#40;"select&#40;)");
    else if &#40;retval&#41;
        printf&#40;"Data is available now.\n");
        /* FD_ISSET&#40;0, &rfds&#41; will be true. */
    else
        printf&#40;"No data within five seconds.\n");

   return 0;
&#125;


Sorry Charles, Michael is right when he says you should be using the arm-linux toolchain. I gave incorrect example.

1. Download & install the correct Codesourcery toolchain for windows which is 'arm-2011.03-41-arm-none-linux-gnueabi.exe'

2. use 'arm-none-linux-gnueabi-g++.exe' or arm-none-linux-gnueabi-gcc.exe'

Jim.