Extern C/C++ confusion in ndk - Syntax error Eclipse - eclipse

I wish to run a C Code in android for which I am using android ndk. I also need to add some libraries. While building these libraries, I am getting errors because of the extern statements in the header files. Eclipse IDE shows syntax errors. I also unchecked everything at Project-->Properties--> C/C++ Code Analysis.
Similar example is shown here. How to resolve this?
#ifdef __cplusplus
extern "C" {
#endif

Related

mex file compiled without errors but not working in matlab

First I wanted to compile MatConvNet library for using in windows form this tutorial
(Compiling MatConvNet on Windows)
but I couldn't. Then I think it is better to compile a very simple file and after that going to compile the library.
I have Matlab R2013a 64 bit and Visual Studio 2010 64 bit.
my program Test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
printf("Hello! :)\n");
}
I can compile Test.cpp in matlab with mex Test.cpp
And when I type test output is Hello! :)
I also can set the correct configuration according to tutorials below and compile it without errors.
1) http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm
2) http://www.orangeowlsolutions.com/archives/490
But when I run it in Matlab, nothing happen. There is no output and Matlab doesn't give me any error.
What is the problem?
Notice that:
in (1) second step is adding "mexversion.rc" from "matlab\extern\include" to the project But this file dose not exist in my computer so I couldn't do it.
In Visual Studio I needed to add tow headers in below for compiling the program.
include "stdafx.h"
include "maxrix.h"
so the Test.cpp in Visual Studio is:
#include "mex.h"
#include "stdafx.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
printf("Hello! :)\n");
}
Pre-compiled header shenanigans
A problem with the Visual Studio version of the code is the pre-compiled header file stdafx.h causing the compiler to ignore any code above it (the mex.h include):
#include "mex.h"
#include "stdafx.h" // ANYTHING above here is IGNORED!
#include "matrix.h"
Move the stdafx.h include to the top or turn off PCH in projects settings and remove the include.
printf vs. mexPrintf
Before getting into MEX project setup, note that printf points to mexPrintf courtesy of mex.h:
#define printf mexPrintf
So, using printf is not an issue, but probably not good practice. The problem comes if you redefine printf after including mex.h or fail to get this define on account of the PCH header.
Regarding MEX in Visual Studio
I posted a more formal guide to setting up a Visual Studio projects for building MEX files as an answer to the more commonly-reference question on this topic, and I would also suggest here to use Visual Studio property sheets to get your project set up to build a MEX file. The details are in the referenced post, but you just need to:
Set the MATLAB_ROOT environment variable.
Create a new DLL project.
Under Property Manager (from View menu), right click on each project's build configuration and "Add Existing Property Sheet...", choosing the MATLABx64.props file from this GitHub repo.
printf only works in native C. You need to use mexPrintf. Therefore, your code should be this:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello! :)\n");
}
In general, printing to standard output in a MEX script doesn't appear in the MATLAB command prompt. If you want to display messages in MATLAB, you need to use mexPrintf instead of printf.
To be explicit, if you consult the mexPrintf documentation, a caveat can be seen towards the end:
In a C MEX-file, you must call mexPrintf instead of printf to display a string.
BTW, I recommend this awesome MEX tutorial here: http://classes.soe.ucsc.edu/ee264/Fall11/cmex.pdf. This is the tutorial that I used to get started with programming MEX wrappers in MATLAB. You'll also see that the first example is of the same "Hello World" caliber that you are trying to get running :)
Good luck!
How To Compile a C/CPP File and Create mex File for Using in Matlab
I found the solution by #rayryeng help and (How to build mex file directly in Visual Studio?) #jorre's post.
This is tested with Matlab R2013a 64 bit and Visual Studio 2010 64 bit.
Test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello World123! :)\n");
}
1.Create a new project in VS -> Visual C++ -> General -> Empty Project.
The name of your Project will be the name of mex file.
You can change it later.
2.Change the Debug to Release.
Right click on the project -> Properties -> Configuration properties -> General
Set Target Extension to .mexw64
Set Configuration Type to Dynamic Library (.dll)
Configureation poperties -> VC++ Directories:
5.Add $(MATLAB_ROOT)\extern\include; to Include Directories
Configuration properties -> Linker -> General:
Add $(MATLAB_ROOT)\extern\lib\win64\microsoft; to Additional Library Directories
Configuration properties -> Linker -> Input:
Add these things to Additional Dependencies
libmx.lib
libmex.lib
libmat.lib
Configuration properties -> Linker -> Command Line:
Add /export:mexFunction to Additional Options
Now you must set you platform to x64 otherwise you'll get error like"Error 1 error LNK2001: unresolved external symbol _mexPrintf".
9.Configuration Properties -> Configuration Manager -> Active Solution Platform -> New -> x64 -> Copy Settings From Win32
Now you can compile your file and get mex file.
If you see these tutorials there are other things thar are not needed and may cause problems.
(http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm)
Create an empty Project, not a MFC Dll Project.
There is no need to *.def file.
There is no need to add "mexversion.rc" into your project.
There is no need to add "MATLAB_MEX_FILE" as a preprocessor definition.

Android JNI c++ files compiling

I'm using JNI in Android development. I want to know where do the compiler find the referenced .h files?
Here in jni/jni_part.cpp, it includs lots of .h files. Opencv related files are defined by the SDK, but the "cartoon.h" is sure to be a project-specified header. But I cannot find it in the project folder. Then do the eclipse compile the JNI c++ code at every run? If so , in which directories does the eclipse find these headers?
Maybe it's basic for JNI but I'm not familiar with it. So plz help me out with one or two sentences. Or you could just paste a simple tutorial for JNI in Android, eclipse.
#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include "cartoon.h"
#include "ImageUtils.h" // Handy functions for debugging OpenCV images, by Shervin Emami.
using namespace std;
using namespace cv;
extern "C" {
.....
This is my eclipse project folder structure
/project
/jni
jni_part.cpp
/src
something.java
You'll need to start using android-ndk to compile the .c, .cpp files to a .so library object, which you can then use in your android project.
Please refer to the Android NDK to get started.
http://developer.android.com/tools/sdk/ndk/index.html#GetStarted
you can find examples in the NDK download.
If Eclipse "knows" where cartoon.h file is on disk, it will help you find it - put the cursor on that #include line and press F3.
Regarding your other question: Eclipse compiles only if the relevant files (sources, headers, or mk) change.

Eclipse giving me Invalid arguments ' Candidates are: void * memset(void *, int, ?) ' though I know the args are good

I am getting an invalid arguments error in eclipse, though I am confident my arguments are good. The suggested arguments contains a '?' which I think may indicate the problem, though I do not know how to fix it.
I have done my best to copy the example I saw here:
http://www.cplusplus.com/reference/clibrary/cstring/memset/
In order to be certain that I am getting the args right.
#include <stdio.h>
#include <string.h>
void foo()
{
char str[] = "why oh why does my IDE give me errors when I know my args are good?";
memset(str, '-', 4);
puts(str);
}
Eclipse gives me the following error on the memset line:
Invalid arguments ' Candidates are: void * memset(void *, int, ?) '
What could be causing this? And what is up with that '?' as the 3rd arg?
Thanks in advance!
PS: Just noticed I am getting similar errors when I try to use operations like malloc, calloc, etc.
In Eclipse:
right click the project
click properties
Expand "C/C++ general" the item in the left hand tree view by clicking the arrow, (just clicking the item itself does not expand the suboptions)
From the suboptions select "Preprocessor Include Paths, Macros etc."
Click the "Providers" tab
Check the box next to "CDT GCC Built-in Compiler Settings [ Shared ]".
Edit:
The reason this works is that there are a bunch of default includes and defines that the compiler silently adds behind the scene when you compile. These instructions get eclipse to grab these otherwise silent preprocessor directives so that it's own indexer is using the same settings
The following method resolves the same problem that I was having. (on eclipse 4.2)
Clean your project (Project -> Clean)
Reindex files (Project -> C/C++ Index -> Rebuild)
Rebuild your project (Project -> Build All)
I think it is something to do with your Eclipse setup, somehow.
Taken standalone, that fragment compiles under GCC (G++) 4.7.1 on Mac OS X 10.7.5 with the command line:
g++ -O3 -g -Wall -Wextra -c ms.cpp
The only surprising thing about the third argument to memset() is that it is of type size_t, but the headers are supposed to declare that, so it should not be an issue.
If you're using malloc() et al, you will be including <stdlib.h>, of course. There is also room to argue that you should be using <cstdio>, <cstring> and <cstdlib>, but that shouldn't stop the code you presented from compiling without error.
If you're working with Visual Studio, size_t is defined as
typedef unsigned __int64 size_t;
In previous versions of the Eclipse CDT, __int64 was not defined. You can fix that issue by adding into C/C++ General -> Paths and Symbols -> Symbols
Symbol: __int64
Value: long long
Or you can upgrade your Eclipse CDT version
I've been using a 3rd party C++ library for BeagleBone development and I tried every possible way to include it (as preprocessor includes, source folder, assember includes, library includes, C++ compiler includes, C compiler includes etc). Refreshen, reindex, clean and built for every change I attempted. I even deleted the project and copied only the .cpp and .h files over to a new project.
I finally found the problem in my setup and rectified it as follows.
Right-click the project > Properties > C/C++ Build > Tool-chain editor > Current Builder:> Select CDT Internal Builder
I'm using g++, for your information.
I had a similar issue with the Eclipse CDT. But in my case the thing was that I had put the using namespace std; statement in a several headers. And in some combination of conditions, when I included all of this headers the Eclipse had such the behavior.
I had a similar issue when compiling someone's code, and the problem was the code style. They defined some methods in this way:
// ... Inside a class
static void
sleep( u32 ms );
I guess this is GNU style. Just changing the declaration to
static void sleep( u32 ms );
removed the issue.

unresolved external symbol - Error using Matlab API

I'm trying to read a .mat-file in C++ with MSVS 2008 but when building a simple program I get the following error:
1>ex3.obj : error LNK2019: unresolved external symbol _matClose referenced in function _main
1>ex3.obj : error LNK2019: unresolved external symbol _matOpen referenced in function _main
I've researched Google as well and it seems that the compiler can't link to the libraries needed for using this functions (matOpen and matClose). I never used an external library before but I tried everything I found in Google to add the Matlab libraries. I did the following:
TOOLS --> Options --> Projects and Solutions --> VC++ Directories --> Show directories for: include files --> then I added the path of the matlab include directory --> C:\Program Files\MATLAB\extern\include
I did the same with the library files: C:\Program Files\MATLAB\extern\lib\win64\microsoft
I also did that for the project:
Right click on the project --> Properties --> Configuration Properties --> C/C++ --> General --> Additional Include Directories --> and added "C:\Program Files\MATLAB\extern\include\win64"
Then I did the same at Linker --> General --> Additional Library Directories --> and added "C:\Program Files\MATLAB\extern\lib\win64\microsoft"
So I really don't know where the problem is. Here is the source code I'm trying to build:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <mat.h>
int main(int argc, char *argv[])
{
const char *file = "mozart_part1.mat";
MATFile *pmat;
pmat = matOpen(file, "r");
if(pmat == NULL)
{
std::cout << "Error: could not open MAT-file!";
return(1);
}
matClose(pmat);
}
Can you see or guess any mistakes I made
Take a look here.
Go through the steps.
What I think you've missed is step 7:
7.Locate the .lib files for the compiler you are using under matlabroot\extern\lib\win32\microsoft or matlabroot\extern\lib\win64\microsoft. Under Linker Input properties, add libmx.lib, libmex.lib, and libmat.lib as additional dependencies.
Edit:
Both Matlab and Visual C++ should be either 32bit or 64bit. There are two options:
Find these 3 lib files from another Matlab which is 32bit. Direct your linker there instead.
Make your Visual C++ 64bit. See here how it's done.
Solution:
What eventually worked was option 2, using this link with instructions.
I solved the Problem!
#Michael Litvin: you were right! I didn't know that you have to switch MSVS 2008 to x64 platform.
As the Matlab libraries are provided as x64 binaries you have to switch your MSVS compiler to x64 as well. I followed these steps to do that: http://software.intel.com/en-us/articles/configuring-microsoft-visual-studio-for-64-bit-applications/
Thanks for you help!

Compiling ffmpeg for iPhone SDK (Symbol(s) not found - Linker)

I'm using ffplay in my Application. I implemented the Librarys which has been used in this Project: http://code.google.com/p/ffmpeg4iphone/downloads/detail?name=ffplay-xproj.zip&can=2&q=
But I've seen that this Sources are very old(Apr 2009). I wanted to Build new Librarys and then change it with these In my project.
What I've done:
Downloaded the ffmpeg Source code: (using command line: svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg)
Compiled the Project with special ./configure options and the gas-processor :
http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2009-October/076618.html
I only changed here the Paths of the actual locations for the iPhone SDK 4.1
Changed the header Path in xCode Active Target Project Settings to my ffmpeg folder
Included the .a files in my project in xCode
Added the Other Linker Flags -lm -lbz2 -lz
Tried to Build my Application, but I get some Linker errors where symbol(s) wasn't found.
Undefined symbols:
"avcodec_init()", referenced from:
And the other errors are nearly the same (_av_codec.....)
How can I build it correctly?
I've found the solution: I set the architecture to Optimized(arm7) and then all was working.
One more Question: I use ffmpeg for streaming an online stream. Which Values Should I set to get a good streaming quality also when I am in 3G. This what i've done:
set the audio-buffer to 1024
set the lowres value of mpeg to 3
Not sure if you are using C or C++ (g++) compiler, but try guarding your #includes for ffmpeg with extern "C": the c++ compiler (if it is used) is probably mangling the function names and hence the link errors.
Try to enclose your include with extern "C":
#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#ifdef __cplusplus
}
#endif