2

My project must be C++. I include:

  • glib-2.0
  • gtk+-2.0
  • pylon (C++)
  • genicam

My other question brought me further.


  1. Can I make #define private so that no other source file wont EVER be able to use the private defines ?

Answered: yes, in C or cpp files (while their headers don't expose those defines)

  1. Do I have to include all libraries twice (for both compiler: GCC C, GCC C++) ? (I will be using C libraries in cpp files). And also for the linker ?

  2. I'm using `pkg-config --cflags --libs glib-2.0 gtk+-2.0` to fix some fatal errors of "file not found". But I still get them (e.g. glibconfig.h ~ file not found). Is there a foolproof fix for that ?

  3. Might the problem be that if I make a global #define null or something and then the included libraries that might have same define names bug around in my project ?


    I started the project step by step including one library at a time and fixing issues if some arose. Then include next library and fix issues etc. I'm using eclipse, I have include guards and mixed c and cpp source files.

3 Answers 3

11

The only way to make a preprocessor macro "private" is to define them in a source file. Then they will be available only to that source file.

You have to remember that the preprocessor (which handles macros and #include directives) is really a separate step that is run before the actual compiler sees the source.

Also, when you define a macro with #define, it's not actually defined in the same way the compiler and linker means it. After the preprocessor stages has run, there are no more macro names in the resulting translation unit.

You might want to read about translation units, and learn how the preprocessor and compiler works.


For your second point, you're not actually building your project twice only once and only with the C++ compiler. So you should only specify the libraries once, when linking your project.

You should also be careful when using e.g. pkg-config to get both compiler and linker flags. First of all you only need the compiler flags (the --cflags argument) when actually compiling your source files into object files. Then when linking you need only the linker flags (the --libs flag to pkg-config), and the resulting flags should be places last on the command line (because the linker wants libraries after the object files that depends on the libraries).

6
  • Aren't c files compiled by GCC C Compiler and cpp files by GCC C++ Compiler ?
    – Bitterblue
    Nov 21, 2013 at 9:06
  • @mini-me But if you're coding C++, then you won't have any C files in your own project, will you? The nice thing about libraries made in C (or which exports a C interface) is that they can be used from almost anything else, especially C++. Nov 21, 2013 at 9:09
  • I still have some c files in my project that have some utility functions. Are you suggesting to rename them to *.cpp ?
    – Bitterblue
    Nov 21, 2013 at 9:13
  • @mini-me Okay, but you compile them to object files linked with the rest of the project, or do you link the C code into a separate dynamic library or program? Or by the phrase "include all libraries twice" do you mean the #include directive? If so, then yes you need to #include the header files if you use them in a source file (no matter if it's a C or C++ source file). How else would the source file know about the declarations in the header file? Nov 21, 2013 at 9:15
  • No, it's 1 big project. And and including twice means in the project settings.
    – Bitterblue
    Nov 21, 2013 at 9:18
2

You can like so

#define X 5
int a = X;
#undef X

Remember that the define is a preprocessor directive, and so is #incldue. The define will ignore scopes and it will be applied to any included file that is between the #define and #undef

This is usually bad practice and can cause maintenance nightmares. Avoid is possible.

1

First off, you might want to learn more about the pre-processor and the compilation process, but now to your questions:

1) Can I make #define private so that no other source file wont EVER be able to use the private defines ?

There is no such thing as a private #define. However, you can use #undef to un-define it after being used or put them only in the .c/.cpp files.

2) Do I have to include all libraries twice (for both compiler: GCC C, GCC C++) ? (I will be using C libraries in cpp files). And also for the linker ?

During the compile step you don't need to specify what library you want to link to later. The compiler is only interested in the header files of the library you use.

The linker then needs the actual library files to link your object files with the library.

3) I'm using pkg-config --cflags --libs glib-2.0 gtk+-2.0 to fix some fatal errors of "file not found". But I still get them (e.g. glibconfig.h ~ file not found). Is there a foolproof fix for that ?

You can do this by setting up your include paths correctly. I don't know how to do this in eclipse, sorry.

4) Might the problem be that if I make a global #define null or something and then the included libraries that might have same define names bug around in my project ?

I doubt that in the case of you header files not being found, but this somehow reminds me of

#define true false

and all those jokes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.