Eclipse CDT c++ project using C headers and doing cross compile - c++

we have an application which is written in c++ and now we are trying to use the same application on the ARM board for which we were provided a toolchain.
So when i do a test c++ project it compiles ok and code on the target is executed perfectly.
As we want to use also some of the functionality of the target (eg display,...), that part is for C, headers are C, libraries are C.
So when i tried including C headers i got a lot conversion errors (eg. Description invalid conversion from unsigned int' tostlv_type' locaion: tlv_tags.h C/C++ Problem)
For example function generating problems:
static inline
enum stlv_type _stlv_get_tag_type(unsigned int tag)
{
return STLV_GET_TYPE(tag);
}
When creating C project this all works without a problem, but c++ project hundreds of conversion errors.
So i tried to include this headers in extern "C" block without any success, still same problems.
So can somebody help me combine this :) as there is no possiblity to get c++ from the manufacturer and also changing headers is not an option as new platform releases can come at any time.
Thanks in advance!

Related

Google Tink library building C++

Trying to build Tink library (https://github.com/google/tink) with Bazel. Bazel installed, gcc version 7.2.0, Windows 10 x64. Visual C++ 2017.
At first, there were errors like "C++ compilation of rule '#boringssl//:crypto' failed" - I commented these lines (with compilation flags I think) in boringssl/BUILD file (sections boringssl_copts, boringssl_copts_c11) and they disappeared.
But after that, bazel said, that error is in errors.h file (https://github.com/google/tink/blob/master/cc/util/errors.h)
// from #include "absl/base/port.h"
#define PRINTF_ATTRIBUTE(string_index, first_to_check) \
__attribute__((__format__ (__printf__, string_index, first_to_check)))
// Constructs a Status object given a printf-style va list.
crypto::tink::util::Status ToStatusF(
crypto::tink::util::error::Code code, const char* format, ...)
PRINTF_ATTRIBUTE(2, 3);
} // namespace tink
} // namespace crypto
enter code here
Error C3646: unknown override specifier on line 32 (line with "PRINTF_ATTRIBUTE(2, 3);"). The most frightening thing is that in another files the same code (defining same attribute) is working.
There are another errors in this file, but mentioned is the first (and another are about the same line, so they are consequences of the first I guess).
I'm nearly a total newbie in cpp, but only cpp should be used, not java-version of library.
Thank you for your help, and sorry for possible misformatting and broken english - this is my first question here.
Unfortunately, we don't support Windows for now. It's something that we plan to support next year, please see our feature roadmap.

xcode fails to build C++ with Unknown type namespace

I have created a C++ project in Xcode 8.2.1 with a Bridging Header file.
I have added a Point3.hpp and Point3.cpp file into a shared folder.
The types for the cpp and cpp files (in the identity and type inspector) are Default - C++ Source and Default - C++ Header respectively.
The LLVM 8.0 Language settings are:
C++11 [-std=c++11]
libc++ (LLVM C++ standard library with with C++11 support)
When I try to build the project, I find that I get the following error:
I have tried searching to find information to resolve this issue, but most seem to be related to compiling source that is a combination of objective-C and C++ whereas this project was setup as a Swift3 project with C++ bridge. Source code
ex3-Bridging-Header.h
#import "../../../Shared/ex3/math/Point3.cpp"
Point3.cpp
#include "Point3.hpp"
using namespace EX3;
Point3::Point3()
{
} ...
Point3.hpp
#ifndef EX3_POINT3_HPP
#define EX3_POINT3_HPP
namespace EX3
{
class Point3
{
public:
Point3(); ...
compiler settings
After continuing looking, I finally managed to find a working solution. There may be other ways to achieve this but I fixed it by routing the C++ through an Objective C layer. Whilst this seems a little bit clumsy (as it adds an extra bridge layer), as of Swift3, it is reported that there is no direct interop layer to C++.

How to make sure code (c++) written in Xcode can compile on other platforms?

I am a beginner was trying to do some C++ programming on Xcode. It works fine, but when I try to compile the same c++ file on my windows pc using VS, there were some errors. After I look at my code closely, there are really some stupid mistakes that I have made which caused the errors, but Xcode seemed to have ignored them...
My question is that is there any setting that I need to change to prevent Xcode from being so smart?
For example, the following code can actually compile in xcode:
#include <iostream>
using namespace std;
int main() {
if (true or false){
cout << "How is this possible? \n";
}
return 0;
}
There are also other cases where the code is actually wrong, but it can compile just fine is Xcode which is the annoying part and I want to disable that.
As far as I can see there is nothing wrong with your code.
The ISO C++ standard does not specify which standard headers are included by other standard headers. So, it is entirely possible that the version of iostream used by Xcode directly or indirectly includes ciso646. Whereas Visual Studio's version of iostream does not include ciso646. There are many similar cases with other headers. You just need to read the error messages and realize that your error (when you move your file to a different platform) is due to a missing header file.
It would be nice if writing portable code meant writing code in accordance with the C++ standard specification, but unfortunately that's not the case. Although there are various compiler options on various implementations which can help bring different implementations closer together, in general you will just have to bring the code into the target environment and actually test it there.
So ultimately writing portable code means you'll have to learn some subset of C++ that is accepted by all the implementations you want to target.
or is an 'alternative token' in C++, and VS is incorrect to reject it. There's no option in Xcode to disable support for alternative tokens. However VS has non-standard support for or as a macro using the header <ciso646>, and Xcode does have a header <ciso646> which does nothing (as the standard specifies). So you can write code which uses or and which works in both Xcode and VS by including this header.
#include <iostream>
#include <ciso646> // does nothing in Xcode, allows `or` in VS
using namespace std;
int main() {
if (true or false){
cout << "How is this possible? \n";
}
return 0;
}
Unfortunately VS can't support all of the alternative tokens through macros and so Xcode will still support some that VS doesn't.
There are also other cases where the code is actually wrong, but it can compile just fine is Xcode which is the annoying part and I want to disable that.
If you give specific examples then I can provide additional advice on how to write portable code.
Rather than changing your Xcode settings, I suggest cross-checking your code using another development environment.
If you're looking for something cheap and full-proof. Download a VirtualBox Windows VM, and run download Dev C++ (bloodhshed)
VS does not support or: you need to use || instead.
You can include some special files but it doesn't inject or sufficiently well into the language for it to work in all instances.
If you want to suppress use of or (and your compiler supports no better way)
#define it to something that emits a compiler error, for example
#define or OR
This at least means that the nature of the compilation errors will be identical on Xcode and VC.

How do I use arduino libraries with standard C code

I am using Eclipse kepler for AVR development.
The code that I have is C (Open Source), and I've gotten it adjusted so it runs perfectly. My target is an ATmega2560, in the form of an arduino mega2560.
Using the arduino board is strictly for hardware convenience; we are developing the hardware to be a custom board with most of the core arduino mega2560 components.
I need to use several libraries with this project that are only available as arduino libraries, namely libraries for an e-paper screen (from seeedstudio) and Nordic's BLE nRF8001.
If I create a new arduino project using the plugin in eclipse, I can build and run the tests for the arduino libraries perfectly.
When I try to merge the 2 code bases together, I can't seem to call the functions in the added arduino libraries - if I call them the compiler throws a linking error.
Building target: Virgin2ManualArdInsert.elf
Invoking: AVR C Linker
avr-gcc -Wl,-Map,Virgin2ManualArdInsert.map -mmcu=atmega2560 -o "Virgin2ManualArdInsert.elf" ./avr/adc.o ./avr/eeprom.o ./avr/lcd_and_input.o ./avr/main.o ./avr/strings.o ./avr/unimplemented.o ./avr/usart.o ./aes.o ./baseconv.o ./bignum256.o ./ecdsa.o ./endian.o ./fft.o ./fix16.o ./hash.o ./hmac_sha512.o ./messages.pb.o ./p2sh_addr_gen.o ./pb_decode.o ./pb_encode.o ./pbkdf2.o ./prandom.o ./ripemd160.o ./sha256.o ./statistics.o ./stream_comm.o ./test_helpers.o ./transaction.o ./wallet.o ./xex.o
./avr/main.o: In function `main':
main.c:(.text.startup.main+0xc): undefined reference to `writeEink'
collect2: error: ld returned 1 exit status
makefile:53: recipe for target 'Virgin2ManualArdInsert.elf' failed
make: *** [Virgin2ManualArdInsert.elf] Error 1
As a test, I'm just trying to call a basic "write to display" call in eInk.cpp from main.c:
extern "C"{
void writeEink()
{
EPAPER.begin(EPD_SIZE); // setup epaper, size
EPAPER.setDirection(DIRNORMAL); // set display direction
eSD.begin(EPD_SIZE);
GT20L16.begin();
// int timer1 = millis();
EPAPER.drawString("testing", 10, 10);
EPAPER.drawNumber(12345, 60, 40);
EPAPER.drawFloat(-1.25, 2, 80, 65);
EPAPER.display(); // use only once
}
Is a static library built from the arduino cores the way to go here? I've tried it (though it seems most of the procedures are outdated) and the libraries do not want to link/be called.
What is the correct procedure for including C++/Arduino calls in my C code?
I've tried using extern "C" {function()}; in my .cpp files and .h files but to no use.
Thank you for any help or pointers to where I can figure it out for myself.
You can try to compile your C code as C++ by simply renaming the files to *.CPP, but chances are that you have to modify your code to make it compile as C++ code. There are things that are allowed for C, but not for C++ (like calling functions that are not declared).
The other solution is to wirte wrappers around the C++ functions that you want to use from C.
You have to consider two limitations of C against C++:
C is not object oriented
C does not support overloading of functions
This example for Serial.print() shows how you can handle this with a wrapper:
extern "C" void SerialPrintInteger( int value )
{
Serial.print( value );
}
In this example you would write similar functions like SerialPrintFloat(), SerialPrintString() etc.
The extern "C" prefix tells the compiler to create the function in a way that makes it callable from C.
The error you received above isn't a compiler error, it's a linker error. I haven't used Eclipse for Arduino development, I just stick with the Arduino IDE, but the standard Arduino projects expect all of your code to be in a single source file, which it compiles and then links with the Arduino libraries. Arduino programs don't have a C/UNIX-style "main" function, the standard functions are "setup" and "loop."
I recommend going back to one of the Arduino example programs, blink for instance, and watching the console log as Eclipse compiles and links the program. What's happening here is:
The C/C++ compiler compiles your source code, including setup(), loop(), and any other functions you have created, into an object file.
The Linker links this single object file with the Arduino runtime, and any Arduino libraries you have specified. The output of this is an image of the program, in your example above it's trying to make 'Virgin2ManualArdInsert.elf'.
The uploader (probably avrdude) loads this image into your Arduino and resets it.
The Arduino comes out of reset and runs your new code.
If your program is reasonably small, say not more than a few hundred lines, just put all the functions in the one source file, then you won't have to learn how to drive the linker.
If you need, for some reason, to have the sources in a separate file (maybe they're shared with another program, or another platform), then you'll have to learn how to get Eclipse to link the object files from your multiple source files. This may just involve adding the sources into your Eclipse project properly, or you may have to write a Makefile or something similar.
As for C vs C++ source code, you can usually drop a C function into a C++ source file and compile it. There are a few differences, but this way you don't need to worry about "C" linkage or any of that silliness.

Set Up iOS project to use eigen

I'm trying to set up an iOS project to compile a c++ library called Eigen for Linear Algebra maths.
I begin following the instructions of the Question mentioned in:
How to compile Eigen in iPhone
But there are still some kind of things I could not figure out.
User Stafan said to set the right compiler flags (-mfpu=neon -mfloat-abi=softfp). Where do I set those flags? I already found out to set flags for a single file like in http://meandmark.com/blog/2011/10/xcode-4-setting-compiler-flags-for-a-single-file/. But for what files do I need to set this flags? Especially the c++ files from the Eigen library do not appear here.
User Blukee said that he used armv7 optimized arch in build setting. In build settings I see in the Architectures category a green field saying Standard(armv7) is that what right?
I just created a ,,singleViewApplication'' and wanted to use the c++ files in an new Objective C class called MathLibary. Because some guys said that it is possible to mix up Objective C and C++ if I name the class MathLibary.hh and MathLibary.mm.
The class looks like
#import "MathLibary.hh"
#define EIGEN_DONT_VECTORIZE
#import "SVD.h"
#implementation MathLibary
#end
When I try to run the App the build fails an the compiler gives out some errors like:
In line 47 in SVD.h file, which is:
typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
The error is Expected a qualified name after 'type name'
For me it sounds as that the compiler tries to compile the SVD.h as objective c file but is c++.
How do I change the compiler to compile both objective c files for the user interface and the c++ files from the library
I hope that somebody is able to help me, thanks a lot.
If you want to mix ObjC and C++, did you make sure to rename your file to have a ".mm" suffix? :)