Chain of C libraries into C++ - 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?

Related

Using C Functions in C++ Using extern "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

Why Xcode gives error 'string' file not found?

I am trying to interop with basic C++ method in Swift. But Xcode gives me an error when I build the project;
'string' file not found
I have researched this issue on StackOverflow but every solution and answers didn't solve my problem. And I started to think that what if I use libstdc++ rather than libc++. Then I found that libstdc++ was removed with Xcode 10. I found the files on Github and apply it to appropriate file directories. And that did not do much either. Did you guys encounter with this kind of problem before?
This is my header file.
#ifndef CPPTest_hpp
#define CPPTest_hpp
#include <string>
std::string saySomething();
#ifdef __cplusplus
extern "C" {
#endif
int returnNumber();
#ifdef __cplusplus
}
#endif
#endif CPPTest_hpp
This is the cpp file.
#include "CPPTest.hpp"
std::string saySomething()
{
return "Welcome to C++";
}
int returnNumber()
{
return 10;
}
This is where I expose header to Swift
#include "CPPTest.hpp"
After all, When I build the project. It gives me an error like;
'string' file not found
Why does this error occur?
By the way, it works when I use integer as return type and its appropriate implementation in cpp file.

Manual CubeMX C to C++ project conversion fails when including FreeRTOS

Toolchain is the SW4STM32, gcc, processor is STM32F303K8 (Nucleo 303K8), minimal test project with no actual user code at all.
The process how to convert a project generated by ST CubeMX is well documented and apparently works. However, when i specify FreeRTOS in Cube, linker fails to find the init function MX_FREERTOS_Init(). The function prototype is included in main.cpp and the definition exists in another source file (freertos.c). This works in C but when converting the project to C++ the linker fails to link the function.
The C2C++ conversion i did as follows:
add ccnature to the .project file
copy main.c to main.cpp (& remove main.c from the build)
duplicate gcc compiler settings over to g++ in project properties
point linker script to the one in the project directory
The above enable Eclipse to compile main using g++ and to link using G++ linker. However linking consistently fails in ...\Debug/../Src/main.cpp:97: undefined reference to `MX_FREERTOS_Init()'
What is remarkable is that even when i delete all references to MX_FREERTOS_Init() from main.cpp so that the text simply does not exist anywhere, linker STILL fails exactly the same. It even reports the same line number even though the text is completely different. This behavior does not change never mind if i clean the project, rebuild and refresh all indexes etc etc.
Any suggestions anyone? Or do i have to skip specifying FreeRTOS in Cube and do it all manually?
I have the same question using atollic for stm32,
solved by adding extern "C" to main.cpp
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config();
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
#ifdef __GNUC__
/* With GCC, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
#ifdef __cplusplus
}
#endif
/* USER CODE END PFP */
I have faced with the same issue.
So, I would like to suggest renaming freertos.c file to freertos.cpp.
I would suggest a C++ RTOS for example the free disortos. (Google it)

Playing cutscenes in c++

I'm using SDL for opening a window and handeling events. And OpenGL to render my objects to the screen. SDL_mixer for sound, and SDL_ttf for text. Now I'm trying to figure out how to be able to put a video on display. Like an animated logo or something before the game starts. Just to experiment with it. At some point I will need to know it...
I've found and tried installing FFMPEG, I've included the "include" folder, and set the "lib" folder. At first I got an error telling me it was unable to load "inttypes.h". So I downloaded a package with that and put it in the include folder for FFMPEG.
Now I'm stuck with this error, which I can't seem to be able to solve.
c:\program
files\ffmpeg-20140507-git-4cdea92-win64-dev\include\libavutil\common.h(87):
fatal error C1004: unexpected end-of-file found
And another question, is there other libraries aviable that may be easier to use for displaying a simple video? I read something about SDL being able to do it, but nothing was to be found about it.
EDIT: Here is line 78 to 96:
#if FF_API_AV_REVERSE
extern attribute_deprecated const uint8_t av_reverse[256];
#endif
#ifdef HAVE_AV_CONFIG_H
# include "config.h"
# include "intmath.h"
#endif
/* Pull in unguarded fallback defines at the end of this file. */
#include "common.h"
#ifndef av_log2
av_const int av_log2(unsigned v);
#endif
#ifndef av_log2_16bit
av_const int av_log2_16bit(unsigned v);
#endif
Wrap your #include <libav*.h>s inside a block like this:
#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#define UNDEFINE_STDC_CONSTANT_MACROS
#endif
extern "C"
{
#include <libavdevice/avdevice.h>
#include <libavdevice/version.h>
}
#ifdef UNDEFINE_STDC_CONSTANT_MACROS
#undef __STDC_CONSTANT_MACROS
#endif
There's probably a way around using the UNDEFINE_STDC_CONSTANT_MACROS hack but the logic just isn't coming to me right now.
Worked for me on VS2012 using Zeranoe's 32-bit ffmpeg development binaries and msinttypes' inttypes.h.
Hmm, I found the DLL-files, and it gave me a linker error with the 64-bit library.
I changed back to 32-bit and the 32-bit dlls and it worked fine. I guess that libraries ask if you have 32/64-bit Visual Studio insteath of Windows.

GCC cross-compiler for VxWorks can't compile C++

I'm trying to port a Linux library to run on VxWorks. I have successfully built binutils and gcc to target i486-wrs-vxworks and I can successfully build a simple C program. However, when I try to compile C++, things break.
I have a simple Hello World program:
#include <string>
#include <iostream>
int main()
{
std::string s = "Hello World";
std::cout << s << std::endl;
return 0;
}
To build it, I call:
i486-wrs-vxworks-gcc -I/home/kyle/vxworks-6.9/target/usr/h -I/home/kyle/vxworks-6.9/target/usr/h/c++ hello.cpp
This always fails with the message:
In file included from /home/kyle/vxworks-6.9/target/usr/h/c++/cerrno:4:0,
from /home/kyle/vxworks-6.9/target/usr/h/c++/xlocnum:4,
from /home/kyle/vxworks-6.9/target/usr/h/c++/ios:4,
from /home/kyle/vxworks-6.9/target/usr/h/c++/ostream:4,
from /home/kyle/vxworks-6.9/target/usr/h/c++/istream:4,
from /home/kyle/vxworks-6.9/target/usr/h/c++/string:4,
from hello.cpp:1:
/usr/local/lib/gcc/i486-wrs-vxworks/4.6.4/../../../../i486-wrs-vxworks/include/yvals.h:4:24: fatal error: yvals.h: No such file or directory
If I go look inside /usr/local/i486-wrs-vxworks/include/yvals.h, this is what I see:
/* yvals.h values header for conforming compilers on various systems */
#if (defined(__cplusplus) && defined(__GNUC__))
/* GCC C++ has it's own yvals.h */
#include_next <yvals.h>
#else /* __cplusplus && __GNUC__ */
#ifndef _YVALS
#define _YVALS
#ifdef _NO_WINDRIVER_MODIFICATIONS
#include <stdarg.h>
#endif
...
It appears that there is another yvals.h that needs to be included, but I can't find it anywhere. Did I just fail at building gcc correctly, or is there a way to fix this?
Which version of VxWorks are you using for this?
I have a fuzzy recollection that when upgrading VxWorks versions in the past there was a syntax error in yvals.h that was I needed to work around and it was fixed in a subsequent version.
Also, you can get the gcc cross compiler pre-built from WindRiver. Just login to windriver.com/support with your licence number and head to "Downloads" for your product version.
I went through a recent cross compiling nightmare myself (not VxWorks related) except that instead of yvals.h, I was having grief with stddef.h. The problem turned out to be that I needed to specify the include paths for the system header files.
Here are the steps it took me to solve my error messages. Feel free to modify as appropriate.
Create a file foo.c
#include <stddef.h> /* The problem header file (yvals.h for you?) */
int main (void) {
return 0;
}
Compile it with your compiler of choice
$(CC) foo.c -E
Note the include paths it uses and set them as your system header file list using the
-isystem <include path>
option.
Hope this helps.