Can I create C / C ++ # set private?

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.

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).

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Can I create C / C ++ # set private?

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录