Why can't I use _Generic in C++ code? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I find that to instantiate a _Generic, the source file needs to be a .c file and compiled with gcc -std=c11. Naming it as a .cpp file and using g++ -std=c++11 does not work. Neither does g++ -std=c11 (which is expected because the -std=c11 switch is only applicable for .c files). What is a good way to define _Generic functions in a C++ library, and let a C++ application use the library? The intent is to support C applications but without abandoning support for C++ applications.

There's no _Generic in C++ and you cannot use it in C++ code. If you want to design cross-compilable code (e.g. header files), either avoid _Generic entirely, or use #ifdef __cplusplus to provide two independent (or semi-independent) versions of the same code for C and C++ separately, e.g. use function overloading on C++ side instead of _Generic.

Related

Can the IDE directly read the C library source code? How to find the source code needed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to learn the source code of floor() in c Function library, so I use the Vs quick lookup definition function to find in VS, and then
I find that _Check_return_ _ACRTIMP double __cdecl floor(_In_ double _X); statement。
I don't know what to do next.enter image description here
Because there seems to be only a macro definition, no function definition。
Is it that I should not use the IDE to view the source code, I am a newbie。
In general, for C, no.
C code can be shipped in pre-compiled form, when you get:
A header file (mylibrary.h) that provides declarations of contents of a particular module of code.
A library (mylibrary.lib, mylibrary.dll, mylibrary.so and so on, depending on the exact type of library and the platform), this contains the code and data that are needed for the module.
These two files are enough; the compiler reads the header when you #include it in your code, and the linker "glues in" the necessary code and data from the library.
Note that there is no source code available.
The floor() function is part of the C runtime library and shipped with the compiler implementation; I'm not sure whether Microsoft provides the source for theirs. Of course there are open source implementations, here is the code from the "musl" implementation of the C standard library, for instance.
Most library functions are visible to your project just as prototypes, as they have been already compiled into a static (or dynamic) library. As far as functionality is concerned, the compiler could ship without those sources, as they are not necessary to compile your program. It's pretty much the same e.g. for OS APIs: you invoke them all the time, but you don't have the Windows sources.
On the other hand, Visual C++ traditionally shipped with the sources of (most of) its C runtime to aid debugging; they are usually found somewhere under your VC++ installation directory.
edit better indications in this answer; thanks #Martin R for digging it out

CPP - Convert source code from Windows to Linux [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a single-file CPP app, built for Windows and is running in command line only.
I am trying to make it compilable also for Linux.
The only libraries the app is using are:
#include <iostream>
#include "setjmp.h"
#include "windows.h"
#include "time.h"
It also uses all kinds of unsigned integers (uint8, uint16, uint32, uint64).
Other than this there aren't any other Windows-specific APIs.
My question is how to convert the code so it will be compatible with Linux?
Is there an easy way to do this?
Do it the other way round. Code, probably using some cross-platform framework like Poco or Qt (which is also usable in non-GUI code, e.g. using QtCore without QtGui), or Boost, in some Windows independent way and compile it regularly on both OSes.
BTW setjmp.h is C++ unfriendly (messing and incompatible with C++ exceptions), even on Windows.
So remove
#include "setjmp.h" /// wrong in all C++ programs
#include "windows.h" //// specific to Windows
then fix the code (e.g. all compilation errors; but on Linux compile with all warnin & debug info, e.g. with g++ -Wall -Wextra -g), probably using some framework like the ones I mentioned.
Don't convert C++ code (from Windows to Linux) but do try hard to write portable C++ code, thanks to some well chosen framework.
Perhaps your application could be written in some pure portable C++11 (but then, no need to #include "windows.h"). Command-line utilities like wc, cat, grep (with a subset of all features) could probably be written in portable C++11 -and might not even require any additional framework.
PS. Without having your source code and without any idea about what that application is, it is impossible to help you more. Your first task is to understand that code precisely and what it is supposed to do on Windows. Perhaps rewriting it in clean C++11 (maybe with some additional framework....) is the quickest way to do it.

What to compile as a library? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've lost my head just a little trying to reason the most effective means of compiling a c++ project. I stick to more managed languages like Java so the CMake file is a bit obtuse to me.
My main problem is what do I compile as a library and what do I just compile together? I have a main function in my program with various over classes in different files with headers. What is the most normal way of handling these files together? Should I compile the main function separate from the classes then link them or should they be a shared library even though it is a bit small for a library?
Mainly I am looking just for general guidelines of what should be compiled together, what should simply be linked, and someone to more clearly explain the norms/best practices of how this all works.
I understand that the compiler needs to convert the Header and Source files to object files and then combines them together as a binary. I am just confused at what should go into the binary.
If you need the code for only one executable you can just link all object files together. Libraries are useful if you need the same functions/object files in different executables.
Of course the bigger a project gets you could also use sub projects which output libraries and then link the main project files and the sub project libraries together.

Install OpenCV on a C++ compiler [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am used with using OpenCV with python. But does someone have an idea how to add openCV library to a C++ compiler (such DevCpp or CodeBlocks...).
If there is a compiler on which it's easier to install OpenCV library no problem, I have no restriction conserning the compiler.
I followed some tutos on the net but they were not so clear.
Thanks.
C++ has two important phases of compilation. First, each individual .cpp file is needed. You need the library header files (.h) for this. Secondly, the separate parts are linked together, and you need the library files themselves. (.lib/.a depending on platform).
So, you need to provide paths to both. The compiler knows which exact headers are needed from the #include statement, but the libraries to link must be explicitly listed.

Why use clang and gcc, your include is different when you want use `int32_t` or `memcpy`? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Why there are different includes between gcc48 and clang when you want use int32_t or memcpy?
when use gcc, after you include <iostream>, you also should include <stdint.h> and <string.h> for using int32t and memcopy
when use clang, you just need include <iostream>, and you can free use int32t and memcopy, stdint.h and string.h automatically included.
Why is that?
And can I run clang as gcc behavior ?
I use Mac OS.
Probably that's because clang's standard library happens to include <stdint.h> and <string.h> already in <iostream> for its own private reasons; this is not guaranteed, and, as you can see, your gcc's standard library doesn't work that way.
Don't rely on such behavior; always include the needed files regardless of these coincidences.