I'm developing a native application using Eclipse for Mac and I can't get std::string or std::map to resolve.
I've already had to add a number of include directories to the path to handle the NDK not being set up that well out of the box; currently I have
.../Development/android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include
.../Development/android-ndk-r9/platforms/android-18/arch-arm/usr/include
.../vuforia-sdk-android-2-6-10/build/include
.../android-ndk-r9/sources/cxx-stl/system/include
set as C++ symbol paths, and #include <string> seems to have worked fine. However, all the references to std::string are not resolving, and Eclipse says
Type 'std::string' could not be resolved.
I know this code builds because I successfully built it on a Windows PC; it's an eclipse problem of some description. What can I do to make these symbols resolve?
EDIT 1: I've looked inside <string> and it seems that it mainly contains a bunch of other includes, for example #include <bits/c++config.h>. All of these seem to resolve, but I am going to see if I can find them and determine where std::string actually lives.
EDIT 2: I can find std::basic_string, but not std::string (yet).
EDIT 3: std::basic_string is not apparently visible to my source code either.
Im not sure if this will be an answer for you. My experiences are different, I'm on Windows, and I was only trying to get C (not C++) code working. But ran into exactly the same issue. And my solution could seemly be used to work for C++ as well. As far as I can tell, Eclipse/ADT on Mac is much the same as on Windows. So that shouldn't be a problem here. Who knows, I could be talking out of my a--.
However, I think the problem is solved two fold. Here is how I solved it. I followed the tutorial: http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/
Things to note:
1) When developing in Eclipse/ADT for Android. It always assumes that your writing in Java. You need to make the project "Mixed Java & C/C++" (not that I've seen it called this outside the tutorial, lol)
2) Your ndk-build should probably work fine from the command line (even tho it might not compile in Eclipse).
3) In the tutorial, the two things that seemed to get it to recognize C/C++ are "Step 5: Convert to a C/C++ Project" followed by Steps 6-8 (which seem like one big step).
4) In Step 7, under "Build command". You might have to use a the complete path (no spaces) to your ndk-build. (*Windows users enter "ndk-build.cmd" here)
5) In Step 8, also include a full path.. no spaces. However, this is where you would put a path in for "GNC C++" instead. (This is my guess for your solution)
6) If all else fails... It should work in C. Try Step 8 exactly, and use only C syntax. Because, at the top of this older tutorial (http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/) it talks about how limited NDK is. It says C++ functionality is limited. Otherwise, this second tutorial is mostly obsolete it seems. Cygwin and command line compilation are not needed anymore, and my Eclipse installation had the C/C++ support out-of-box. However, its all still usable/valid.
PT
(and on a similar note, and for those seeking help with NDK. Also make sure a space-less full path to your NDK folder is entered in at Eclipse > Window > Preferences > Android > NDK > NDK Location)
Related
My program needs to read jpeg-images from a file native. For that purpose I downloaded official jpeglib c-source code and that works flawlessly on a Windows version of this program, which is programmed using Embarcadero RAD Studio 8.
But when I try to use the same source code in Android Studio JNI, I see lots of errors almost every c-source file :( Below is a screenshot:
As you can see, there's errors on both
-Macro expansions (ERREXIT, red underlines), which complain "class 'blahblah' doesn't have a field 'blahblah'".
-member methods (red text in code), which complain also "class 'blahblah' doesn't have a field 'blahblah'".
Looks like there are many C-language classes and fields, or probably all, which are not working. But the jpeglib source definitely contains them all, something is just not working in Android Studio! Probably Android Studio C-compiler is somehow limited, but there must be some way to bypass it...
This problem doesn't exist in C++ source files. I have different source folders for java and C++ source files (and there are also separate jni folder, but I don't use it, probably I should), but C source files resides inside the C++ folder. I have added all c, cpp, h and hpp source files inside the parentheses of the CMakeLists.txt's add_library() directive, but that's all. I don't know is it enough...
I'm glad to get any suggestions how to fix this.
It is not enough to add all libjpeg files to your add_library(). The library expects to be configured for the specific toolchain, see e.g. this example.
I prefer an optimized version of the library, called libjpeg-turbo. On GitHub you can find step-by-step instructions how the library can be cross-compiled for Android.
Well, there seems to be happy ending...
I have to admit that Android Studio most likely can compile JPEG c-library:) The problem "class 'blahblah' doesn't have a field 'blahblah'" was because the library needs to be configured for the specific toolchain, like #Alex Cohn said. But the configuration doesn't happen in CMakeLists.txt, instead you need to rewrite the code itself at some extent. For example, you need to change
-INT32 to int32_t
-#define boolean bool
-#define EXTERN extern
-add lots of #include <> and #include ""
-and more...
If you need to do that, start from header files: don't touch C files until you have headers error free. As a side note, I had exactly the same build order in Android Studio than I had in Embarcadero Studio, but that didn't helped to succeed... I don't know yet, will this modified library work, but at least I think I will get rid of compilation errors!
NOTE: it apparently is a recurrent question on StackOverflow, but - for what I have seen - either people never find a way or their solution does not work for me
The problem:
I am using Eclipse Juno ADT. Everything was working fine until I tried to update the NDK. I replaced my ndk folder (that was the ndk-r8d) by the new version (i.e. ndk-r8e) and, in my Paths and Symbols configuration, I changed the includes to go from g++ 4.6 to 4.7.
It seemed to break my index: I could compile my code, but Eclipse was giving semantic errors, exactly like in [1] and [2]. The errors mainly come from symbol used by OpenCV4Android, such as distance, pt, queryIdx and trainIdx.
When I tried to backup to my old configuration, the index was still broken! I cannot find a way to change this.
What I have tried
Clean up the project
Rebuild, refresh, and all the other options in the "Index" submenu (when "right-clicking" on the project)
Disable / enable the indexer in the preferences
Verify that symbols such as trainIdx only appear in my OpenCV4Android include in the Paths and Symbols section.
Change the order of my includes in the Paths and Symbols section. I basically tried to put the OpenCV include in the beginning and in the end.
Some observations
What is not working
I assume that it is the CDT index because of the following:
In command line, I can build my project using ndk-build clean and ndk-build.
When I start Eclipse, I have no error until I open a C++ file (from the jni folder).
I can always build the project, but as long as I have opened a C++ file, I can't run the application anymore because of a lot of Field '<name>' could not be resolved.
If I don't open the C++ files, Eclipse doesn't report any error and can build and deploy the Android application successfully.
Interesting fact
The following code reports errors on line, queryIdx, pt:
cv::line(mRgb, keypointsA[matches[i].queryIdx].pt, keypointsB[matches[i].trainIdx].pt, cv::Scalar(255, 0, 0, 255), 1, 8, 0);
If I write it as follows, it works:
cv::DMatch tmpMatch = matches[i];
cv::KeyPoint queryKp = keypointsA[tmpMatch.queryIdx];
cv::KeyPoint trainKp = keypointsB[tmpMatch.trainIdx];
cv::line(mRgb, queryKp.pt, trainKp.pt, cv::Scalar(255, 0, 0, 255), 1, 8, 0);
It is not that all of the OpenCV functions are unresolved: only pt, queryIdx and trainIdx are.
Any comment will be really appreciated.
In your selected project preferences within the Eclipse environment, go to C/C++ General -> Code Analysis -> Launching. Make sure that both check boxes are unchecked. Close and reopen the project or restart eclipse and rebuild the project.
Since indexing for Android native code on Eclipse is incomplete, I managed to enable indexing in my NDK projects the following unintuitive way, it should work whether you use ndk-build or plain make or even cmake. I'm using Kepler but it should work on older versions too.
Get your toolchain right
Right click on project -> Properties -> C/C++ Build -> Tool Chain Editor -> Uncheck Display compatible toolchains only.
In the same window, set Current toolchain to Linux GCC.
In the same window, set Current builder to Android Builder if you're using ndk-build, set it to Gnu Make Builder otherwise (this step may be wrong, sorry in advance if it is).
Right click on project -> Properties -> C/C++ Build -> Build Variables -> Make sure Build command reads the correct command for your project; if it's not, uncheck Use default build command and correct it (it may be ndk-build or make -j5 that you want). If you build the native code in a separate terminal, you can skip this step.
Make a standalone toolchain, it's probably the cleanest way to get STL sources in one place
Go to the NDK root directory.
Run the following (tweak the settings according to your liking). Add sudo if you don't have write permissions to the --install-dir because the script fails silently.
./build/tools/make-standalone-toolchain.sh \
--platform=android-14 \
--install-dir=/opt/android-toolchain \
--toolchain=arm-linux-androideabi-4.8
This is assuming that you use GNU-STL. If you use another C/C++ library, you will need to tweak the above command, and probably also the include paths in the next command.
Add the necessary include paths to your project
Right click on project -> Properties -> C/C++ General -> Paths and Symbols -> Go to the Includes tab -> Select GNU C++ from Languages -> Click Add and add the following paths (assuming you installed the standalone toolchain to /opt/android-toolchain):
/opt/android-toolchain/include/
/opt/android-toolchain/include/c++/4.8/
/opt/android-toolchain/include/c++/4.8/arm-linux-androideabi/
/opt/android-toolchain/lib/gcc/arm-linux-androideabi/4.8/include/
/opt/android-toolchain/include/c++/4.8/backward/
/opt/android-toolchain/lib/gcc/arm-linux-androideabi/4.8/include-fixed/
/opt/android-toolchain/sysroot/usr/include/
Here, you can add every include path you want. In fact, I have my OpenCV built for Android and installed in the standalone toolchain, so I have the following include there:
/opt/android-toolchain/sysroot/usr/share/opencv/sdk/native/jni/include/
Now, the indexing should work. You should also be able to run ndk-build (or make if that's your build method) and then deploy your project to your device inside Eclipse.
Why?
Android native development on Eclipse is incomplete since the indexing doesn't work out of the box. This is due to having to support multiple architectures (ARMv7, Intel etc.), multiple STL options, multiple Android versions etc. This is why you have the bare make based ndk-build and the whole NDK structure, and this is also why NDK development is very unclean and few large volume native Android projects exist.
A big Android project is OpenCV where they had to develop a 1500 odd line CMake script to get it to compile for Android properly. At some point, they tried to export that script as a CMake based build system for Android but it couldn't keep up with the changes in the NDK system and was abandoned. This support should have been inside NDK itself.
The default NDK build system should have been standalone toolchain only, with all different architectures/C++ libraries having their own toolchains at the cost of storage space but with the advantage of cleanness, intuitiveness and good practice. Then you can incorporate any standard cross-compilation system that is also used elsewhere, is tested and is well-known, e.g CMake. You can, and in my opinion you should, do that with the NDK's make-standalone-toolchain command as shown above. But in the end, this is only my opinion. If you feel comfortable enough with ndk-build then go ahead.
It's actually quite hard to say what is the problem. Here are some advices:
Try to import and build hello-jni (it is located in jni's samples folder). If it runs without problems than problem is with linking OpenCV to your project.
It seems that you forgot to update android-ndk location in project properties -> c/c++ build -> environment. Here's link to problem Issue with build Android NDK project.
Build from console your project (ndk-build -B), delete all errors in Eclipse manually (in Problems view select all errors and just click delete) and try to run project now. Sometimes this "hack" helps me to run project.
Close Eclipse and delete folder path-to-your-workspace/.metadata/.plugins/org.eclipse.cdt.core (backup it first).
Go to Preferences > C/C++ > Language Mapping > ADD (Source C File and select GNU C) Do the same for C++
I had the same issue. I had all the proper include paths setup but after opening the .c/.cpp or .h file and it would start marking everything as "Unresolved."
This worked for me...
Go to:
PREFERENCES -> C/C++ -> INDEXER
Check Index Source And Header Files Open in Editor.
I had the same issue, like many people.
I followed the steps in Ayberk Özgür post, which make good sense. Although I also had to make sure to put includes under all three languages: GNU C, GNU C++, and Assembly. Probably because I'm not using a stand alone tool chain.
I at first had my includes only under GNU C and GNU C++ languages. Which left me still with the unresolved includes error. Not until I assigned my includes under the Assembler language as well did my errors go away.
I do not know why eclipse is only searching through the Assembler includes in my project. I also do not know how this part of the solution will work for bigger more complicated projects.
Hope this helps.
I had the similar situation with Eclipse CDT working with the OpenCV library. I got several error messages while the program compiled correctly. I changed the indexer setting in "window->preferences->Indexer" "build configuration for indexer" box to "Use Active Configuration" which solved my issue.
I just spent about 3h banging my head against this Eclipse NDK indexing issue!..
What made it work: make sure that you have only ONE cpu architecture specified in Your Application.mk file.
Otherwise the .metadata/.plugins/com.android.ide.eclipse.ndk/*.pathInfo file will not be generated by the NDK build. This file contains built-in values from Project -> Properties -> C/C++ General -> Paths and Symbols -> Includes (just making .pathInfo file does not fix the problem)
I'm working on an app which uses NDK (all I'm writing happened both on r6b and r8d)
Everything was working fine, and I wanted to start and try debugging my C code.
I followed this http://tools.android.com/recent/usingthendkplugin tutorial,
but NDK_DEBUG = 1 tag to my build command, suddenly I started getting errors in the code which didn't go away even after removing that tag, changing from Android 4.2.2 back to 2.2, changing the NDK I was using, or anything else I could think of.
The problems happens now inside statements like this
#ifdef __ANDROID__
some cool android code
#else
some pretty awesome iOS code
#endif
what happens it that the __ANDROID__ is for some reason not define, causing eclipse and ndk-build to try and compile the iOS code instead of the Android's
Reverting everything I did didn't seems to have any effect. Restarting eclipse didn't as well. Cleaning the project, completely delete libs and obj directories didn't work too..
Any suggestions?
Thanks!
EDIT:
Maybe it's worth adding that the build itself, using ndk-build completes successfully.
I think it might be an eclipse issue, but even if so, it still an error and I can't launch the app
Also, just in case, restarting the computer didn't work either.
EDIT 2:
The problem exists on another computer running the same workspace over network, my guess was something related to the workspace, so I tried deleting .metadata folder and adding the project again.
Deleting the .metedata folder fixed it the 1st time, but after a few minutes (in which I managed to build and run the app on my tablet) the same issue returned, and deleting the .metadata didn't work
EDIT 3:
Still no go.
However, I can confirm that it's not a project specific problem, as all the projects that has Native support in eclipse now do this.
Other things that doesn't work:
creating an empty project, adding Native support.
Completely changing to another unrelated workspace and perform the above tests
Downloading fresh version of eclipse (juno), CDT & ADT (was using the eclipse ADT bundle)
Just so you know:
when you work with Android.mk file(s) and ndk-build, the ANDROID macro is predefined (see -DANDROID extra C flag when building with verbose outputs),
but if you use the Android Standalone Toolchain, then __ANDROID__ is predefined instead.
So I suggest you to use:
#if defined(ANDROID) || defined(__ANDROID__)
/* ... */
#endif
Apparently __ANDROID__ is a specific GCC macro that it supposed to define internally whenever correct options are provided. However, since control over options is largely delegated to NDK, one should not rely on __ANDROID__ macro being ever defined. The compiler behind NDK might not be GCC for all we know (or care). When working with NDK, check for ANDROID.
Edit: clang now also defines __ANDROID__ macro
If anyone else encounters this issue:
I opened the workspace on another computer (Workspace is in a Dropbox folder), and the problem was still there on the other computer, which could only mean a workspace issue, so I delete the .metadata folder from the workspace.
I had to re-add the projects, but after doing so, everything seems to work now.
I have been given a program written in C, and have been asked to use the functions from the program in a new Android application. I have been following the tutorials of the NDK, but have hit a wall. I think all that is left is compiling or linking my C files, but I'm not sure. I have installed the SDK, NDK, Eclipse, Cygwin, and Sequoyah ( I haven't used Sequoyah yet and don't really know where it comes into play ).
I have used the ndk-build command, but am not sure what I am should be expecting. This is a screenshot of my Cygwin terminal.
This is a screen shot of my NWPTest.c file. All of the fields defined in the .h file come up as errors. I feel like this is an error that should go away once I compile the code.
BYTE and WORD etc. declarations here:
This is a screen shot of the NWPTest.h file. BYTE and WORD declarations are also coming up as errors, which is something I do not understand.
This screen shot basically shows the last thing as the last one, but I'm confused how some of the BYTEs are errors and some are not:
EDIT
Here is my Android.mk file. Just to clarify, I was given the entire C project, and was told to make it an Android application. I have no experience with C.
Probably your problem is that Eclipse doesn't know about the include path for the ndk C header files; however that should not really matter since ndk-build appears to have built your native library. Though if your C-aware eclipse setup is being obstinate and refusing to build the rest of your project with "errors" in the C files, then you will have to either fix the include path or tell eclipse to ignore the C files as it does when not aware of C.
Of more concern would be whatever is causing an error in Operations.java - that will prevent you from building an apk.
I guess this is eclipse error. It simply does not understand your c and h files. Try to install CDT plugin, maybe after that the problems will eliminate.
This is really frustrating
- I can build my native code from command line, but when I build from eclipse(Sequoyah plug-in enabled) its simply through simple compilation errors like headers not found...
EVen when i build the library from command line everytime I try to run from eclipse it rebuuilds and there goes errors again
- I'm frustrated as I ran out of option to locate the issue
Can some one shed some light on this.
The error you are seeing is Unresolved inclusion with error markers at each header that Eclipse's editor cannot find. This is confounding when you see it, because it is expected that after installing Sequoyah and the ADT, pointing the Sequoyah configuration to your NDK, that you'd have everything you need to start coding.
Two things to observe. The process of building in the ADT "Android Perspective" will work until you click on one of your C/C++ files in your jni directory. Once you open one of these, you'll see the error marker and the project will be tagged as containing errors.
Second observation, when you convert the project to C/C++ perspective or to Sequoyah's Android Native perspective (apparently there's two ways to skin this cat), you will have the ability to configure the project settings around NDK toolchain, include paths, and builder settings. This is where you can set the ndk-build to automatically fire off on each change. And funny thing too is that the ndk-build will work fine until you click on one of your C/C++ sources.
So solution, click [here] http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.cdt.doc.user/tasks/cdt_t_proj_paths.htm and you'll get instructions for configuring the CDT's include paths. You want to set your include paths for C/C++ (either, or both) so that you get to the platform folder includes.
Example, I've got my project hello-jni-to-the-world project set to android-9. So configure the include path: android-ndk-r6b/platforms/android-9/arch-arm/usr/include . Now the magic won't show up until you click apply/save and you'll be prompted to rebuild the indexes.
There are two to three other threads on Stackoverflow asking the same question, and I'll have to find them and add them to the comments. Basically, there were no definitive answers and there's a lot of the usual answering a question with a question: which version of NDK do you have, can you post your code, did you install java, is your computer on ?