cvra / platform-abstraction

Platform abstraction layer for microcontrollers
3 stars 6 forks source link

C is not 100% compatible with C++ #20

Closed pierluca closed 10 years ago

pierluca commented 10 years ago

I only now realized that we're doing something a bit naughty :-)

We're writing C code and testing it after compilation by a C++ compiler. A thing which we're doing, I presume, because of the unit testing framework.

This is far from being an innocuous change as the two languages are not 100% compatible.

Among other things, literals (such as 'L' or '3') are not the same type (char vs int), const globals are treated differently and functions are not compiled in the same way.

What about using a C-only testing framework? It might require minor overhead but at least we're only working on pure C and can safely ignore the idiosyncracies of C++.

antoinealb commented 10 years ago

The C code is compiled with a C compiler if you use CMake. Only the tests are C++ (but calling pure C code), so this is not an issue in my opinion.

antoinealb commented 10 years ago

Plus, pure C testing framework, such as Unity are a real pain to work with, as C doesn't provide any form of introspection.

pierluca commented 10 years ago

I agree that C testing framework (SeaTest, ACE, etc.) aren't very comfortable. They're not too much of a nightmare either. SeaTest is fairly straightforward for instance.

It is quite possible that I have misunderstood though. As I understand it, all code is compiled as C++.

Where do you see that the .c / .h are compiled with gcc and not g++ ? I can't find any reference to such difference in any of the files for cmake or travis.

antoinealb commented 10 years ago

So, for the .h you have the `extern "C" to tell g++ not to do any name mangling on those symbols.

For the .c, you can see by doing make VERBOSE=1 instead of simply make and you will see the following output which clearly shows that C files and C++ files are compiled with the correct compiler.

/usr/bin/c++    -g   -o CMakeFiles/tests.dir/tests/main.cpp.o -c "/home/antoine/Code/Robotique/Coupe 2015/platform-abstraction/tests/main.cpp"
/usr/bin/cmake -E cmake_progress_report "/home/antoine/Code/Robotique/Coupe 2015/platform-abstraction/build/CMakeFiles" 2
[ 50%] Building CXX object CMakeFiles/tests.dir/tests/platform_mock_test.cpp.o
/usr/bin/c++    -g   -o CMakeFiles/tests.dir/tests/platform_mock_test.cpp.o -c "/home/antoine/Code/Robotique/Coupe 2015/platform-abstraction/tests/platform_mock_test.cpp"
/usr/bin/cmake -E cmake_progress_report "/home/antoine/Code/Robotique/Coupe 2015/platform-abstraction/build/CMakeFiles" 3
[ 75%] Building C object CMakeFiles/tests.dir/mock/semaphores.c.o
/usr/bin/cc   -g   -o CMakeFiles/tests.dir/mock/semaphores.c.o   -c "/home/antoine/Code/Robotique/Coupe 2015/platform-abstraction/mock/semaphores.c"
/usr/bin/cmake -E cmake_progress_report "/home/antoine/Code/Robotique/Coupe 2015/platform-abstraction/build/CMakeFiles" 4
[100%] Building C object CMakeFiles/tests.dir/mock/mutex.c.o
/usr/bin/cc   -g   -o CMakeFiles/tests.dir/mock/mutex.c.o   -c "/home/antoine/Code/Robotique/Coupe 2015/platform-abstraction/mock/mutex.c"
pierluca commented 10 years ago

Okay, I'm not sure I understand how this is configured but if it's compiling correctly, I'm happy. Thanks for clarifying this for me :-) ... and sorry to have wasted a bit of your time :-(

antoinealb commented 10 years ago

The magic here really is CMake. It is very good at detecting which compiler it should use.

and sorry to have wasted a bit of your time

Don't be ! I prefer to spend some time explaining / checking for possible issues rather than really wasting my time searching for weird bugs that are compiler-induced ! ;)