as the title suggests I am attempting to mix C and C source files in a project.
My project has the following files
.
├── comms
│ ├── can.c
├── config_parsing
│ ├── config_parser.c
│ ├── file_operations.c
├── main.cpp
├── scheduler.c
├── signal_handler.c
├── thread_health.c
└── utilities
├── logger.c
├── ring_buffer.c
├── string_operations.c
├── time_conversions.c
In eclipse this compiles, builds and deploys. I am adding code from a vendor that I cannot share as it is purchased code. They have code for SSL which has .h
and .c
files. In the header file we have
#include <openssl/err.h>
#include <string>
#include <vector>
In the C file we have many instances of std::vector<unsigned char>
, std::string
. This does not compile.
I am using a Yocto generated SDK to build my application and I can confirm the appropriate files exist in my include paths
/opt/fsl-imx-fb/5.10-hardknott/sysroots/cortexa7t2hf-neon-poky-linux-gnueabi/usr/include/c /10.2.0
When looking online I see how to properly include C header files in a C application but I cannot seem to find out how to use std::vector
in a C file. If needed I can share images of the eclipse compiler/build settings.
The application builds in Ubuntu.
CodePudding user response:
It's not possible to use std::vector
in a C file, because C doesn't have namespaces, classes, or templates.
The closest it gets is that C code could get a pointer to the vector's contents, and pass the pointer to C code, which could read or write data there, because C does have pointers.
CodePudding user response:
if you want to provide an answer, I will gladly accept it! The -c c option has worked! – Michael
It is pretty clear that the code in the .c
files is C code and not C code. This is evidenced by the #include
directives you found:
#include <string>
#include <vector>
The compiler front end (e.g. gcc
or g
) will try to determine the language to use based on the file extension (e.g. .c
--> C and .cpp
--> C ).
So, even if we compile with g
it will still compile as C because of the .c
extension (vs. prescanning the content of the file).
So, there are a few remedies:
- Rename all
.c
files that are actually C code to.cpp
- Add
-x c
to the compiler command line for the affected files to force it to interpret the files as C code. This works for either front end:gcc
org
Ordinarily, (1) would be the chosen method if the files were "owned" by you. That is, fix it the right way once.
If we could convince the 3rd party vendor to do this, that would be best (i.e.) the vendor applies (1).
However, if the vendor won't/can't do this and you/your company renames the files, the problem returns anytime you take in a new version from the vendor.
So, a cleaner (i.e. one time fix) is to do (2) and modify your build/Makefile to add the -x c