Where are the CURLOPT_* constants defined for the curl_easy_setopt function? - libcurl

I've looked at practically every file included in the libcurl source package and can't seem to find where the CURLOPT_* options are defined. I gather that they're probably integers, perhaps an enum, but for the life of me I can't find them.
The language I'm writing in is RealBasic, if that matters at all. Generally when using an external library written in C I need to manually find and translate the various #define blocks in the headers. But I have to know where the #define block is before I can do anything!

They're defined with the CINIT() macro within the curl/curl.h header file. In a very recent such file (as of me writing this) they start at line 782.
The macro actually creates a line within a big enum construct.

Related

Is there a way to assign a value to a macro by reading a value from another file?

I am working on C++ project. I have a file named "version" where only 12343 like number is defined [In fact the version] in numeric form.
This "version" file is defined in Library A.
Somehow I need to assign this value to a macro in another library B file.
Is there any way to do it?
Since, the work is official, so I can't share the code here.
Neither C nor C++ has a language feature that would allow you to set the expansion text of a macro to something read at compile time from an external file. Such tasks are generally assigned to the build system, instead.
There are diverse mechanisms for the purpose, but they follow this general procedure:
An external program reads the file containing the wanted text
That program either
formats the text of a compilation option conveying the definition (e.g. -DVERSION=${what_I_read_from_the_file}), or
writes a header file containing the macro definition itself, e.g.
#define VERSION version-text-from-the-file
Either way, the obtained information is provided to the build in the appropriate manner -- the compiler option alternative proceeds by ensuring that the chosen option is passed to the compiler at build time, whereas the header alternative depends on the files that need the macro #includeing the generated header.

Why is it not advised to define macros in header files?

The Google C++ Style Guide guide advises that macros must not be defined in a .h (header) file. What are the cons of doing it?
The preprocessor concatenates all included source files together in order. If you don't undefine a macro, it can apply to any source following where it was first defined.
Since headers are often the public API of a library, any macros you define in your headers could end up in someone else's code, doing unexpected things.
Since unexpected things are the antithesis of good software, you should either:
Not use macros (idiomatic C++ really shouldn't)
Define them in a private scope (always prefer private) or
Undefine them immediately after use (although this makes them largely useless for your own code)
The best solution depends on your use case. Include guards and other simple, safe defines are typically excluded ( function-like macros are more likely to cause problems, but you can still do something dumb like define TRUE FALSE).
You may also look into conditionally defining macros so they are present in your code but don't become part of the public API. Checking for a variable set during your build or keeping macros in a separate header allows others to optionally, explicitly, and knowingly include them, which can be convenient if the macros help avoid a lot of boilerplate.
For the same reasons that using statements should not be in header files: namespace pollution. If you want to use macros in a header file, make sure that you undefine them at the end of the header, this way they will not be included erroneously. If you simply want to define them in a header and use them in cpp files make sure that the "macros.h" is never included in any header.
The who point of this is that a end user of what ever public API you are developing may not want or expect, for example, sum(a, b) to expand to (a) + (b). Finding the source of one's own macro error can be a nightmare, finding someone else can be almost impossible.

Header and Code in C++ classes

I just started on a few C++ tutorials, and I have run into something that I just can't seem to make much sense of.
In C++ it seems people are using a code file and a header file, for me this just seem inconvinient. Why would I want to swap around between two files just to write a simple getter method.
Is it considered the "correct" way to use headers in C++? Or is it just the tutorial I have picked up that uses this?
I get the idea of splitting code to make it look more clean, but is it good for anything else other than that?
Thanks in advance.
There are some reasons for using hpp(header)- and cpp(code)-files. One of them is the following: A library (dll- or so-file) cannot be "used" like a jar-file in java. If you write a library, you have to provide declarations of the classes, methos,... in form of a hpp-file.
Think about using the class you wrote in other files. If you had the class definition in a separate file, you could help the compiler to figure out how to use the class by including the header file in places where you are planning to use this code.
The compiler only needs to know whether you are using the classes right(it does not care about how to run it, until linking), therefore all you need to give the compiler is the declaration of the class(header file), to do the error checking. When you say "include", the preprocessor just copies and pastes the header file contents into the new file, so that the new file now knows how to use the class you wrote.
A header file in c++ stores alot of information, if c++ have been made using every single "header" file in c++ in each program you make, when you then write a function from iostream for example, the program will go through every single header file just to find the right header file. so instead they made the #inlcude function in c++, so you could specify where your functions are from.
And when you create a program you could make own header files, so the code is more nicely set up. and then instead of having to make alot of lines of code in one main source file, you could import others. like if you are making a game, one header file for Animals and in that header file you have a Class for Cats, and one for dogs. having a more clean code.
In C/C++, headers are used to share the class structure (among other things) between classes.
so one can use
include "classFOO.h"
in classBAR.h (or classBAR.cpp) and use classFOO.

How and where to define pre-processor directives such that they are accessible wherever we want in the project?

The intention here is that when the program starts, a particular function will read a configuration file and set some #defines. In other parts of this project, these preprocessor directives will decide what code to execute and what not.
Example:
A file X contains:
#define WHAT 0
A file Y contains:
#if (WHAT)
// Do this
How and where should these types of #defines be organized so that they are accessible where they should be without creating a mess?
Preprocessor directives are resolved when the program is compiled, not when it starts, so what you're asking for can't be done.
You'll need a runtime mechanism to make this work, but that doesn't guarantee code exclusion from the compiled binary.
The intention here is that when the program starts, a particular
function will read a configuration file and set some #defines. In
other parts of this project, these preprocessor directives will decide
what code to execute and what not.
As the other answer has said, this is not possible as the preprocessor directives like #define are consumed by the compilers pre-processors. What your executable binaries actually see is the compiled modified source which remains the same irrespective of every time you run with a different file that you open. Moreover, there is no concept like loading a configuration file and changing the run time as, C++ is a compiled language and not an interpreted.
What actually is possible is to
load the configuration file (preferable in a stand format)
Parse it with publicly available libraries for standard format or write your own parser.
Use STL objects like map to create a mapping between the configuration key and value
Place the STL in some namespace so as not to pollute the global namespace and make it extern. Ensure that an extern declaration is present in a header file and the variable is defined in a .cpp file so that the variable can be accesses from a translation unit different from where it was defined.
Consume the mapped configuration anywhere within your program.

Ways not to write function headers twice?

I've got a C/C++ question, can I reuse functions across different object files or projects without writing the function headers twice? (one for defining the function and one for declaring it)
I don't know much about C/C++, Delphi and D. I assume that in Delphi or D, you would just write once what arguments a function takes and then you can use the function across diferent projects.
And in C you need the function declaration in header files *again??, right?. Is there a good tool that will create header files from C sources? I've got one, but it's not preprocessor-aware and not very strict. And I've had some macro technique that worked rather bad.
I'm looking for ways to program in C/C++ like described here http://www.digitalmars.com/d/1.0/pretod.html
Imho, generating the headers from the source is a bad idea and is unpractical.
Headers can contain more information that just function names and parameters.
Here are some examples:
a C++ header can define an abstract class for which a source file may be unneeded
A template can only be defined in a header file
Default parameters are only specified in the class definition (thus in the header file)
You usually write your header, then write the implementation in a corresponding source file.
I think doing the other way around is counter-intuitive and doesn't fit with the spirit of C or C++.
The only exception is can see to that is the static functions. A static function only appears in its source file (.cor .cpp) and can't (obviously) be used elsewhere.
While I agree it is often annoying to copy the header definition of a method/function to the source file, you can probably configure your code editor to ease this. I use Vim and a quick script helped me with this a lot. I guess a similar solution exists for most other editors.
Anyway, while this can seem annoying, keep in mind it also gives a greater flexibility. You can distribute your header files (.h, .hpp or whatever) and then transparently change the implementation in source files afterward.
Also, just to mention it, there is no such thing as C/C++: there is C and there is C++; those are different languages (which indeed share much, but still).
It seems to me that you don't really need/want to auto-generate headers from source; you want to be able to write a single file and have a tool that can intelligently split that into a header file and a source file.
Unfortunately, I'm not aware of any such tool. It's certainly possible to write one - but you'd need a given a C++ front end. You could try writing something using clang - but it would be a significant amount of work.
Considering you have declared some functions and wrote their implementation you will have a .c/cpp file and a header .h file.
What you must do in order to use those functions:
Create a library (DLL/so or static library .a/.lib - for now I recommend static library for the ease of use) from the files were the implementation resides
Use the header file (#include it) (you don't need to rewrite the header file again) in your programs to obtain the function definitions and link with your library from step 1.
Though >this< is an example for Visual Studio it makes perfect sense for other development environments also.
This seems like a rudimentary question, so assuming I have not mis-read,
Here is a basic example of re-use, to answer your first question:
#include "stdio.h"
int main( int c, char ** argv ){
puts( "Hello world" );
}
Explanation:
1. stdio.h is a C header file containing (among others) the definition of a function called puts().
2. in main, puts() is called, from the included definition.
Some compilers (including gcc I think ) have an option to generate headers.
There is always very much confusion about headers and source-files in C++. The links I provided should help to clear that up a little.
If you are in the situation that you want to extract headers from source-file, then you probably went about it the wrong way. Usually you first declare your function in a header-file, and then provide an implementation (definition) for it in a source-file. If your function is actually a method of a class, you can also provide the definition in header file.
Technically, a header file is just a bunch of text that is actually inserted into the source file by the preprocessor:
#include <vector>
tells the preprocessor to insert contents of the file vector at the exact place where the #include appears. This really just text-replacement. So, header-files are not some kind of special language construct. They contain normal code. But by putting that code into a separate file, you can easily include it in other files using the preprocessor.
I think it's a good question which is what led me to ask this: Visual studio: automatically update C++ cpp/header file when the other is changed?
There are some refactoring tools mentioned but unfortunately I don't think there's a perfect solution; you simply have to write your function signatures twice. The exception is when you are writing your implementations inline, but there are reasons why you can't or shouldn't always do this.
You might be interested in Lazy C++. However, you should do a few projects the old-fashioned way (with separate header and source files) before attempting to use this tool. I considered using it myself, but then figured I would always be accidentally editing the generated files instead of the lzz file.
You could just put all the definitions in the header file...
This goes against common practice, but is not unheard of.