Using C Functions in C++ Using extern "C" - c++

Iam trying to access two of my C functions in a Cpp code using extern "C". I am using it as follows :
#include <stdio.h>
#include <stdlib.h>
#include <cstdio>
extern "C"
{
#include "pltfrm.h"
#include "xil_printf.h"
}
int main()
{
init_platform();
print("Hello World\n\r");
print("Successfully ran Hello World application");
cleanup_platform();
return 0;
}
Functions "init_platform" and "cleanup_platform" are declared inside "pltfrm.h"
Iam getting "Undefined reference to" error on both functions.What am i doing wrong? iam compiling using eclipse build project option for c++ empty project template.

That message comes from the linking step, when all source files are compiled and the linker tries to connect all functions to the function calls.
You probably need to link to some library or add the source files, for the c-headers you added, to your project.

#Gotiasits Answer was helpful in this case.I removed extern C and it seems native support is there. Will run the executbale in target and check

Related

C++ project build, but IDE shows error

Error: cannot open source file "GL/glew.h"
I have the following code :
//Include GLEW
#include <GL/glew.h>
//Include GLFW
#include <GLFW/glfw3.h>
//Include the standard C++ headers
#include <stdio.h>
#include <stdlib.h>
//Define an error callback
static void error_callback(int error, const char* description)
{
...
I took from there: http://www.41post.com/5178/programming/opengl-configuring-glfw-and-glew-in-visual-cplusplus-express#part4
In order to have a somewhat portable solution, before I even started Visual Studio 2013 I created two System Environment Variable in windows.
GLEW=C:\Install\Development\C++\Framework\glew-1.10.0-win32\glew-1.10.0
GLFW=C:\Install\Development\C++\Framework\glfw-3.0.4.bin.WIN32\glfw-3.0.4.bin.WIN32
So in my project I could for instance write a additional include folder as: %GLEW%\include
As I said, it builds fine and runs fine as well.
Yet, not having intellisense behave properly is really annoying.
How to fix it?
My syntax was actually wrong, you cant use global environment variable in VS using %<name>% but you have to use $(%<name>).
Wherever I wrote %GLEW%\include I should have $(GLEW)\include.
It's working fine now.
Though I'm completely clueless why it built.
This post: https://stackoverflow.com/a/11543754/910813 got me to remind that.

Chain of C libraries into C++

I have a very trivial problem including a chain of C libraries into a C++ main project. I've experience with C but it's the first time that I'm programming in C++.
The structure of the project is a single folder with inside:
main.cpp
Mylib_1.c
Mylib_1.h
Mylib_2.c
Mylib_2.h
main calls -> Mylib_1.h that calls -> My_lib2.h
//main.cpp
#include "Mylib_1.h"
//Mylib_1.h
#include "Mylib_2.h"
main contains both Mylib_1 and Mylib_2 functions and typedef structs
Mylib_1 uses typedef structs and functions of Mylib_2
Everything inside each Mylib_x.h is wrapped between extern "C", like this:
#ifndef __MYLIB_X_H
#define __MYLIB_X_H
#ifdef __cplusplus
extern "C" {
#endif
mycode
#ifdef __cplusplus
}
#endif
#endif
But when I try to compile it with eclipse kepler on Ubuntu 12.04 x64, I get:
Mylib_1.h error: Mylib_2_type_t does not name a type
main.cpp error: Mylib_2_function1 was not declared in this scope
...
Only the above sections are marked as error in eclipse, the header looks included fine.
Furthermore according to eclipse, the __cplusplus flag is false into Mylib_2.h but true into Mylib_1.h
Thinking of some eclipse error, I've tried to manually build the project via g++ (v4.6.3) but I got the same exact problem when I've tried to link the libraries .o with the main.cpp
Seems stupid but I can't figure out what could it be. Any suggestion?
Thank you
Have you checked that your lines
#ifndef __MYLIB_X_H
#define __MYLIB_X_H
are really different for the two files,
e.g. _MYLIB1_H and _MYLIB2_H?

Trying to study shine MPEG Layer-III encoder - getting "redeclaration of C++ built-in type 'bool'"

Greetings.
I am studying the way mpeg layer-III encoding works for an upcoming project. I downloaded the shine encoder as it is said to be the simpliest of all. http://www.mp3-tech.org/programmer/sources/shine.zip is the link.
I successfully compiled them in a standalone project but i need to be using them in a QT project.
I made new blank console project in QT
and added as existing all the files that previously successfully compiled for me (files from shine.zip).
This is my main.cpp:
#include <QtCore/QCoreApplication>
#include "main.h"
int main(int argc, char *argv[])
{
// QCoreApplication a(argc, argv);
// return a.exec();
mainc(argc,argv);
}
This is main.h:
#ifndef MAIN_H
#define MAIN_H
#include "main.c"
#endif // MAIN_H
everything else is untouched (i mean, without those two files it compiled successfully and worked)
I am now getting error at this part
#ifndef bool
typedef unsigned char bool; <--- "redeclaration of C++ built-in type 'bool'"
#endif
Before there was no error here. From what i understand a presence of one cpp file makes all the code compile as c++ and the shine code is c, not c++... Does it mean i cannot use c code in a project that uses QT classes QCoreApplication?
You can mix C and C++ code in the same project, but you need to compile the C code with a C compiler. Rather than trying to include main.c from a C++ file, compile the C code separately, and declare any C functions you need to call from C++ as extern "C", for example
extern "C" int mainc(int argc, char *argv[]);
Never include the implemenation file in the header file. The
#include "main.c"
is wrong. It would lead to an include recursion, if the #ifdef MAIN_H would not protect.
In your example QCoreAppplication is included twice what leads to the error message.

PDCurses TUI C++ Win32 console app - Access violation reading location

I have downloaded pdcurses source and was able to successfully include curses.h in my project, linked the pre-compiled library and all good.
After few hours of trying out the library, I saw the tuidemo.c in the demos folder, compiled it into an executable and brilliant! exactly what I needed for my project.
Now the problem is that it's a C code, and I am working on a C++ project in VS c++ 2008.
The files I need are tui.c and tui.h
How can I include that C file in my C++ code? I saw few suggestions here
but the compiler was not too happy with 100's of warnings and errors.
How can I go on including/using that TUI pdcurses includes!?
Thanks
EDIT:
I added extern "C" statement, so my test looks like this now, but I'm getting some other type of error
#include <stdio.h>
#include <stdlib.h>
using namespace std;
extern "C" {
#include <tui.h>
}
void sub0()
{
//do nothing
}
void sub1()
{
//do nothing
}
int main (int argc, char * const argv[]) {
menu MainMenu[] =
{
{ "Asub", sub0, "Go inside first submenu" },
{ "Bsub", sub1, "Go inside second submenu" },
{ "", (FUNC)0, "" } /* always add this as the last item! */
};
startmenu(MainMenu, "TUI - 'textual user interface' demonstration program");
return 0;
}
Although it is compiling successfully, it is throwing an Error at runtime, which suggests a bad pointer:
0xC0000005: Access violation reading location 0x021c52f9
at line
startmenu(MainMenu, "TUI - 'textual user interface' demonstration program");
Not sure where to go from here.
thanks again.
If I'm not mistaken (and I could easily be), it's due to the difference in calling conventions for C/C++. Try making the callbacks extern "C", and make them call a C++ function. Call it a trampoline :)
Finally got it working. The solution was in the steps below:
First I renamed tui.c to tui.cpp
For the header tui.h, I followed the exact same step of wrapping the code as described here.
then in my project i just included the header without any extern "C" block
#include "tui.h"
Compiled and it worked!

Errors thrown from stl when compiling a module which uses the "Meschach" library

I'm working on a module which uses a shared library, which in turn has a static library linked to it. The shared library build works fine and generates a .so. When I try to use it in the module, I get a variety of errors, most of which are based on stl (stl collections to be specific), at the compilation stage. The errors look like:
In file included from /usr/include/c++/4.3/list:68,
from /home/gayan/LHIMo/LHI/src/CalcEngine/include/JuncNodeInfo.h:11,
from /home/gayan/LHIMo/LHI/src/CalcEngine/include/RiverFlowParameter.h:11,
from Main.cpp:11:
/usr/include/c++/4.3/bits/stl_list.h:465:11: error: macro "catch" requires 3 arguments, but only 1 given
This is given in most places which use list, vector or map.
Please help me to resolve this.
Sample code: "CalcEngine.h" in the library:
#ifndef LHI_CALCENGINE_H_
#define LHI_CALCENGINE_H_
extern "C"{
#include <matrix2.h>
}
class CalcEngine{
public:
protected:
};
#endif /* LHI_CALCENGINE_H_ */
Main.cpp in the application:
#include <iostream>
#include <CalcEngine.h>
#include <list> // The compilation fails as soon as this is added
int main(int argc, char** argv){
return -1;
}
I feel this has something to do with the matrix2.h file but could not pinpoint it. The file could be found here
Doing some googling it seems like the Meschach library has a macro called catch (defined err.h indirectly included by matrix2.h) causing c++ code having exception catching to fail.
Try
#undef catch
after you are done including the meschach headers and see if works better.