Chapter 2
IN THIS CHAPTER
Getting and using CppDroid
Working with other mobile IDEs
Using CppDroid to write code
Getting CppDroid help
At one time, developers relied exclusively on desktop systems to perform useful tasks because desktops provided the required computing power. Laptops came next, but essentially a laptop is a smaller form of a desktop. Today, however, developers rely on all sorts of mobile devices to write code. Even though someone could conceivably use a smartphone for the task, the majority of this activity occurs on high-powered tablet computers. The reason relates not so much to the power, but the form factor. A tablet offers more screen real estate to see your code and observe how it works. Keeping these two goals in mind and looking at the available Integrated Development Environments (IDEs), this chapter relies on Google CppDroid to make the leap from desktop systems to Android-powered tablet systems, such as the ASUS ZenPad 3S 10.
However, you shouldn’t get the idea that CppDroid is the only game in town. You also find a description of a few other offerings in this chapter, and you can certainly try them if you like. The consistent issue with all of these offerings, though, is that they all currently lack C++ 20 support, so some book examples won’t run on your tablet at all. If you want to ensure maximum compatibility with the book’s code, procedures, and screenshots, you still need to rely on Code::Blocks running GCC.
After you get CppDroid installed, you need to know how to perform some basic tasks with it. This chapter doesn’t provide a complete tutorial on using CppDroid, which is why it also discusses how to obtain help. However, you do discover how to interact with the book’s code in this chapter, which is an essential part of the learning experience.
Many IDEs are available for you to use to work with C/C++ code. However, most of them rely on the Windows, Linux, Mac OS X, and Solaris platforms (with Solaris appearing as an option far less often than the others). In addition, most of them are paid options, with Code::Blocks (http://www.codeblocks.org/
) and Visual Studio Code (https://code.visualstudio.com/
) being notable exceptions. However, to program on your Android device, you need an IDE that works with Android and provides some sort of cloud-based storage for the most part (PC-based IDEs use local storage). CppDroid offers a good Android-based solution that you can use in both online and offline mode without problem. Plus, the free option actually does work (but with limits; see the “Free versus paid software” sidebar for details). The following sections give you insights into working with CppDroid.
You can find a number of C/C++ IDEs for Android in the Google Play Store. However, the choices come down to three products for most people (in order of preference):
If you want to develop 3-D games, you must use DirectX through Wine (https://www.androidpolice.com/2020/01/21/windows-compatibility-layer-wine-hits-v5-0-on-android/
) or OpenGL (https://developer.android.com/guide/topics/graphics/opengl
). There are add-ons, such as Unity (https://developer.android.com/games/develop/build-in-unity
) and Unreal (https://docs.unrealengine.com/en-US/Platforms/Mobile/Android/index.html
), but they actually layer on DirectX or OpenGL, so you’re still using one of these two technologies, despite using them indirectly. Using any of these products on Android is difficult, and you should plan plenty of time to integrate these APIs into your IDE.
It’s helpful to know precisely what CppDroid provides. Table 2-1 lists basic functionality, whether this functionality comes only with the paid version, and a brief overview of what you obtain with the basic functionality. As you work with CppDroid, you encounter some deficiencies, especially when running the standardized code in this book, but you also discover that you can run a lot of it without any sort of modification.
TABLE 2-1: CppDroid Features
Feature |
Paid Only |
Description |
---|---|---|
Add-ons manager |
Even though CppDroid comes with all the basics you need, at some point you’ll want to go beyond the basics, which is where add-ons come into play. An add-ons manager makes the task of knowing what you need to add a lot easier. Plus, you can easily get rid of items that you no longer need. | |
Auto indentation |
Trying to keep your code readable means using indentation to see things like the start and finish of an | |
Auto pairing |
Locating a missing parenthesis or brace can drive you slowly nuts. Configurable auto pairing means that you determine how elements are paired, but the IDE helps you ensure that nothing needed to compile the code is missing. | |
Auto updates |
Getting the latest software updates helps you write code that works with the newest trends in C/C++ development. You also get bug fixes, which is essential for the reliability and security of the code you create. | |
C/C++ code examples included |
X |
Because working with tablet-based IDEs can sometimes come with quirks, having a full set of C/C++ code examples is essential. These examples show how to work around the quirks so that you can execute your C/C++ code with just a few small modifications when necessary. |
C++ tutorial and learn guide included |
X |
If you plan to work offline, it’s essential to have a tutorial and learning guide for those times when you almost, but not quite, remember how to perform a particular task. Of course, you’ll also want to keep this book handy. |
Code complete |
X |
Automatically suggests how to complete statements that you type based on previous content. This feature reduces potential typos and makes you considerably more efficient, especially when working on the tiny keyboards found in tablets. |
Compile C/C++ code |
In some cases, such as when working with a web-based IDE, the C++ code you create is interpreted by ROOT (see | |
Dropbox support |
X |
Sharing your code with others is a lot easier when you have Dropbox support. |
File and tutorial navigator |
This feature provides an index into the documentation to tell you about C/C++ code constructs, including variables and methods. | |
Google Drive support |
X |
Working from anywhere on a single piece of code means having access to that code from every environment you use. If your desktop system also supports Google Drive, you can switch between your desktop and tablet as the need arises. |
Portrait/landscape UI |
A tablet presents a constrained screen real estate environment. When an IDE forces you to use it in landscape mode only, you often see the IDE informational panes at the expense of seeing the code. Working in portrait mode lets you ignore most of the IDE panes while focusing on the code. | |
Problem fix suggestions |
X |
You get suggestions for a variety of coding issues, even if those issues may not necessarily result in a compilation error. |
Real-time diagnostics (warnings and errors) |
X |
Real-time diagnostics enable you to find certain classes of errors in your code without having to compile it. The IDE monitors what you type and can point out issues like typos without compilation, which saves considerable time. |
Smart syntax highlighting |
Highlighting makes your code stand out so that you can see things like variables and keywords more easily. | |
Static analysis |
X |
Static analysis helps locate truly difficult-to-find bugs that include: memory leaks, mismatching allocation and deallocation, uninitialized variables usage, and array index out-of-bounds errors. |
Theme-based code syntax highlighting |
X |
Themes let you highlight code syntax in a manner that makes sense to you. If you have visual problems, using themes can turn a difficult viewing experience into one that works well with your vision. The use of themes means that no one is stuck using a particular theme to highlight syntax; you see it the way that works best for you. |
Works offline |
The ability to work without an Internet connection means that you gain flexibility in where you can work. However, it also means that you must have access to everything you need as part of the local installation, which is something that CppDroid provides at the expense of additional local storage use. |
You obtain CppDroid from the Google App Store by searching for CppDroid. Unfortunately, it doesn’t support every version of Android, so you may not actually see it if your device doesn’t support it. Figure 2-1 shows how the page appears when you find it. To obtain a copy, all you need to do is tap Install.
FIGURE 2-1: Locating CppDroid in the Google Play Store.
After the CppDroid app installs on your tablet, you see the Open button as usual. However, instead of opening the app, you see something like the view in Figure 2-2. To work offline, CppDroid needs to install a number of libraries on your system. This process can take a while, so just wait for it to complete.
FIGURE 2-2: Loading the CppDroid libraries for offline use.
You aren’t limited to working with CppDroid, even though it’s the tablet IDE used for the book. Most tablet IDEs will let you perform a basic set of tasks that will work well for the majority of the book examples. The only time you’ll encounter difficulty is when working with examples that use new C++ features, rely on graphics in some way, or employ standard features not found in the tablet IDE. One of the advantages of these alternatives is that they might support your device when CppDroid doesn’t. The following sections tell you about the best alternatives that provide maximum compatibility with the book examples.
C4Droid has many of the same features as CppDroid. For example, it compiles your C/C++ code, so you don’t need ROOT support. However, you can use it if desired. As with CppDroid, the app targets the educational market, but C4Droid doesn’t enjoy the strong community support that CppDroid does (see the article at https://www.androidrank.org/compare/c4droid_c_c_compiler_ide/cppdroid_c_c_ide/com.n0n3m4.droidc/name.antonsmirnov.android.cppdroid
for details). In contrast to CppDroid, no free version of C4Droid exists, but when compared to the price charged for most desktop IDEs, C4Droid is a bargain.
If you’re looking for a single IDE that can do everything you need on your tablet, Android IDE (AIDE) (https://www.android-ide.com/
) might be what you need. Unlike the other IDEs listed in this chapter, this one works with a slew of languages, including Java, C/C++, HTML5, CSS, and JavaScript. AIDE is also Android Studio and Eclipse compatible (limited to API level 27), so if you plan to create Android apps using a language such as Java, this might be the right choice for you. (Unfortunately, Google is focusing on the Kotlin language for Android development and has no plans to add Kotlin support to AIDE now.)
However, with such a flexible range of features comes complexity, which seems to be the major criticism of AIDE. The well-designed tutorials tend to help a little, but obviously not enough for a novice developer. Many users also complain that there is a plug-in for every need and all the plug-ins are paid, so this IDE can nickel-and-dime you to death.
You can use a web-based IDE from any device, including your desktop, so in some cases, they represent the best in terms of device compatibility. A web-based IDE also provides an interpreted environment through ROOT in most cases. Consequently, when learning to develop apps in C/C++, you get instant feedback, which can save considerable time. As shown in Figure 2-3, the web-based offerings also tend to provide a simple interface that allows you to get right to work.
The example in Figure 2-3 is JDoodle (https://www.jdoodle.com/online-compiler-c++17/
), which is one of the best C/C++ online offerings. This particular online IDE supports 72 programming languages. How well it supports all of them depends on the interpreter used. For the most part, you find that the JDoodle IDE provides an acceptable method of working with the code in the book. Because it also supports C++ 17, you can also run more of the examples than you can using a C/C++ app.
FIGURE 2-3: Web-based IDEs tend to provide a very simple interface.
http://cpp.sh/
https://www.codechef.com/ide
https://ideone.com/SXNfC0
https://www.onlinegdb.com/online_c_compiler
https://rextester.com/l/c_online_compiler_gcc
https://repl.it/languages/cpp11
https://www.tutorialspoint.com/compile_cpp11_online.php
After you have CppDroid downloaded, you want to begin working with it. The following sections get you started with the basic features you need to work with the examples in this book. However, the IDE provides a lot more functionality than you find here, so spending time with the various examples and tutorials is a good idea as well.
When the libraries are finally loaded, you see a screen similar to the one shown in Figure 2-4. The top left of this screen displays the name of the file (which you can change if you want). The top right contains buttons to Save, Compile, and Run your app.
Along the bottom of the screen, you see the current phase of working with your code:
FIGURE 2-4: Accessing the basic CppDroid user interface features.
FIGURE 2-5: Use the menu to locate the CppDroid features and options.
CppDroid comes with both examples and tutorials you can use to learn more about the IDE and C/C++ in general. The tutorials work much like the examples—just with more content. To access the Hello World example, choose … ⇒ Project ⇒ Examples ⇒ C++ ⇒ For Beginners ⇒ HelloWorld. The display will now contain the code shown in Figure 2-6.
FIGURE 2-6: Loading an example provides a quick way to see code in action.
To compile this code, you touch the lightning icon. After it has compiled, you can run it by tapping the right-pointing arrow. The display will change to show the output. To clear the output, tap the left-pointing arrow in the upper left corner of the display.
You can place the source code for this book on your Google Drive or Dropbox. Of course, you’ll still need some method of accessing it. The following steps assume that you use Google Drive, but they also work with Dropbox. (When working with Dropbox, you place the code in the Dropbox\Apps\CppDroid
folder.)
Choose … ⇒ Project ⇒ Open ⇒ From Google Drive.
You may have to log in at this point. After you log in, you may see a dialog box like the one shown in Figure 2-7 in which you give permission to access Google Drive from CppDroid. Tap Allow to allow the access. (This is a one-time step.)
Locate the folder containing the code you want to access.
You see one or more .cpp
files. For example, when working with the book’s source code, you might choose the BookI\Chapter03\SayHello
folder.
Highlight the file you want to open and then tap Select.
CppDroid opens the file for you. Figure 2-8 shows an example of the HelloWorld.cpp
file for Book 1, Chapter 3.
At this point, you can compile and run your application just as if you used Code::Blocks. The only difference is that you’re doing it on your tablet.
FIGURE 2-7: Give permission to access your Google Drive.
FIGURE 2-8: The file is available for use with your local copy of CppDroid.
To begin creating a new source code file, you choose … ⇒ File ⇒ New. When you create a new file, CppDroid automatically gives it a default name. You can change the name by choosing … ⇒ File ⇒ Rename. A single file can be part of a project, but you can also make a single file the entire project. For example, a Hello World app would consist of a single file.
You can store your source code locally, on Google Drive, or on Dropbox. When working online, the process is the same as when working with online source as described in the “Working with a simple online project” section of the chapter. The following list tells how you can store your source code locally to make it available at all times.
FIGURE 2-9: Define a new local project.
When you compare CppDroid with Code::Blocks, you find that CppDroid provides a much simpler interface with far fewer features. It works as a means to write code while on the road and for testing simple applications. You can’t use CppDroid as a full-fledged development environment simply because it doesn’t contain the features that such an environment provides, especially when it comes to things like debugging. In fact, the limits clearly present themselves on the Actions menu shown in Figure 2-10, where CppDroid limits you to completing code, performing analysis, compiling, and running the code with or without arguments.
FIGURE 2-10: The list of actions in CppDroid is somewhat limited.
No matter how simple and straightforward the interface, no matter how many examples and tutorials supplied, every app will generate some number of questions. Consequently, you need access to help at some point to make things work. The following sections offer a quick overview of the help available for CppDroid.
The oddest part about working with CppDroid is that there isn’t an actual Help file. When you open the … ⇒ Help menu, you see the options shown in Figure 2-11.
FIGURE 2-11: A list of Help sources for CppDroid.
The CppDroid blog contains the latest entries by the app author. What the blog provides is a running commentary of the problems that the developer is seeing and what is being done to fix them. You also see side posts on topics such as the number of people currently using CppDroid and other projects that the author is contemplating. Even so, this is where you go when you have a problem with the product and hope that the developer is addressing it. Figure 2-12 shows an example of the sort of blog posts you see.
FIGURE 2-12: The developer uses blog posts to help you find bug fixes.
You can find a lot of articles about CppDroid online on various websites. The articles provide you with insights on how to use CppDroid and often answer questions that users have about it. In addition, you can find help using CppDroid at these sites:
https://www.reddit.com/r/cpp/search?q=cppdroid
https://sourceforge.net/
(search for CppDroid)https://stackoverflow.com/search?q=CppDroid
https://androidforums.com/apps/cppdroid-c-c-ide.5356/
The free examples often provide you with insights into how CppDroid works. For example, you may wonder how the static analysis feature works. To see a demonstration of static analysis, choose … ⇒ Project ⇒ Examples ⇒ C++ ⇒ For Developers ⇒ Static Analysis. After the file loads, choose … ⇒ Actions ⇒ Analyze. Figure 2-13 shows the results.
FIGURE 2-13: Use an example to see how the static analysis feature works.
Notice that the output shows various problems with the code, such as the printf format string requires 2 parameters, but 3 are given
at line 43, column 0 near the bottom of the screen. The output helps you locate problems with your code and fix them before you compile it.
The tutorials provide a multistep process for working with C++ within CppDroid. When you choose … ⇒ Project⇒ Tutorials ⇒ C++ ⇒ For Beginners, you see two tutorial options:
Both tutorials give you help with getting over the C++ learning curve from within the CppDroid environment. The IDE changes to show a tutorial outline in the left pane and the associated text in the right, as shown in Figure 2-14. You work directly from within the CppDroid environment, which means that you can better understand how CppDroid works when you finish.
FIGURE 2-14: The tutorials take you through basic processes within CppDroid.