Question
I'm trying to compile C++ code in a Tizen Native project. Specifically, I want to add and compile a library written in C++. While Tizen's main native language has been ANSI C since 2015, I need to incorporate C++ code.
I attempted to add a class to a Native project, but the Tizen environment doesn't parse it correctly. Is there any way to configure a Tizen Native project to accept C++ code in Tizen IDE? Note that I don't intend to use C++ for creating UI or specific Tizen features.
Answer
Problem Understanding
The user wants to integrate C++ code into a Tizen Native project, which primarily supports ANSI C. The main challenge is configuring the project to properly compile C++ code.
Solution Methods
-
Basic Configuration:
- Start with a BasicUI project and change the source file extension from .c to .cpp
- Go to Project Properties → C/C++ Build → Settings → C++ Compiler → Dialect → Language Selection and choose C++11
- In Tizen Settings → Toolchain Info, select GCC 4.9
-
Adding C++ Classes:
- Add class header and source files in the project's inc folder
- Include the header in your main project source file (located in src folder)
- Create and use objects of your class
-
Troubleshooting Compilation Errors:
- If you encounter errors like "expected '=', ',', ';', 'asm' or 'attribute' before ':' token", ensure:
- Your file extensions are correct (.cpp for source, .h for headers)
- The compiler is properly configured for C++11
- All necessary include paths are set
- If you encounter errors like "expected '=', ',', ';', 'asm' or 'attribute' before ':' token", ensure:
Code Examples
Here's a simple C++ class example that should work when properly configured:
// line.h
class Line {
public:
void setLength(double len);
double getLength();
Line();
private:
double length;
};
// line.cpp
#include "line.h"
Line::Line() : length(0) {}
void Line::setLength(double len) {
length = len;
}
double Line::getLength() {
return length;
}
Additional Tips
- When porting external C++ libraries, pay attention to:
- Library paths
- Proper linking of dependencies
- Any platform-specific requirements
- Consider using extern "C" wrappers if you need to interface between C and C++ code
- For more complex scenarios, refer to the official documentation on integrating external libraries