Name conflict with standard library - c++

My project uses a third party modules where one of the header file has defined 'errc'
typedef int errc;
I want to use STL in project but when I add stl header file I get name conflict for errc since its class name in the standard library.
error C2872: 'errc' : ambiguous symbol
I don't really want to change the third party module, is there any way I can come around this problem and work with the standard library in the project?

Don't use in the file
using namespace std;
But you can still include it into functions, say
void f()
{
using namespace std;
cout<<endl;
}

Related

How to link static library without using -I option

I have created C++ static library using object files (which has many sub directories). After creating static library created a C file and a wrapper file (header file which I have under C++ directories). Now I'm trying to compile the C file by linking the C++ static library it gives error for the header file
error : no such a file or directory.
When I use -I option (-I C++ header files location) compiled successfully and able to run. But I would like to use the static library without including header files location i.e only adding static library itself C program should compile successfully.
Below is the source:
Edit:
I have the below files under libbasic folder:
testdemo.h
#ifndef TESTDEMO_H
#define TESTDEMO_H
#include<iostream>
#include<string>
using namespace std;
class testdemo
{
public:
testdemo();
void CallingTestDemo();
};
#endif // TESTDEMO_H
testdemo.cpp
#include "testdemo.h"
testdemo::testdemo()
{
}
void testdemo::CallingTestDemo()
{
`cout <<" CallingTestDemo!!!!!!\n";
}
testbasic.h
#ifndef LIBBASIC_H
#define LIBBASIC_H
#ifdef __cplusplus
#include<iostream>
#include<testdemo.h>
using namespace std;
class Libbasic
{
public:
Libbasic();
void Display();
void DisplayName(char* name);
};
#endif
#ifdef __cplusplus
extern "C" {
void displayfromC();
void displayfromCName(char* name);
}
libbasic.cpp
#include "libbasic.h"
void displayfromC()
{
Libbasic llb;
llb.Display();
}
void displayfromCName(char* name)
{
Libbasic lb;
lb.DisplayName(name);
}
Libbasic::Libbasic()
{
}
void Libbasic::Display()
{
cout <<" C called C++ API \n";
testdemo td;
td.CallingTestDemo();
}
#endif
#endif // LIBBASIC_H
I compiled the above program and created library libbasic.a
Now Im creating C API file outside the libbasic folder to call the above functions used in C++
testApi.c
#include<stdio.h>
#include <libbasic.h>
int main()
{
displayfromC();
}
Now im trying to create output using the below
gcc -o testdemo testApi.c -L ./libbasic -lbasic
Which giving libbasic.h: no such a file or directory error.
The basic Idea is create a library and API functions which can be used in any machine. If I have multiple folders and header files in C++ code then need to include all the folders while creating C application which requires to export header files too. I dont want to expose all the source to other users.
Kindly let me what mistake im doing and also how to achieve this.
A header file is not a wrapper.
If you have functions in a library, whether static or dynamic, you must have their declarations (often called "prototypes") in your code.
That is how C and C++ work so header files are integral part of any library.
As for the location of header files, these languages give you three options:
Use <filename.h> for standard include header path.
This path depends on your OS and compiler, but as long as your header file is there, you will not need to specify the path explicitly in compilation command.
Use "filename.h" for files in your local source directory, i.e. same directory where your *.c or *.cpp files reside.
The compiler can look there automatically as well, but if the header is in a sub directory, you will need to specify the relative path in your include statement.
#include "mylib/myheader.h"
Add additional search path for header files using a parameter (such as -I for gcc).
Finally, similar rule applies to the library object file it self (*.a, *.lib).
You will need the -L parameter in addition to the -l parameter if your library is not in a standard library path known to the linker.
Note that you never specified which compiler you are using, so I am guessing based on parameter names it is from gcc family or similar.
A way to alter the search path and tell the compiler where to find your library files will differ based on compiler type, but the principle is the same.

Namespace for external library in C++

I am trying to link to an external library, on which I have no control, that doesn't have any namespace for its functions.
Since I don't want to have conflicts with the names defined in that library: how to include the external headers in a namespace that I would create myself?
I know that the following code doesn't work, but the spirit of the question is there:
namespace extLib {
#include "externalFolder/externalHeader.h"
}
If you are working with a header-only library, your mentioned approach will probably work. At least I can't think of any issue right away.
But if you have a compiled library that you have to link to, there is no way to put the library functions themselves into their own namespace (at least not without recompiling your own version of said library). That's because in the .dll or .so or what have you, each function has a mangled name that includes all namespaces (example). When you eventually link against the library, you can only "reach" those functions under that exact mangled name, which requires that your function calls are against that same (or, in your case, no) namespace as the compiled versions.
The "classic" workaround is to write a thin wrapper around the library, where for every exposed function, you do:
wrapper.h:
namespace libraryWrapper
{
void bar(int);
}
wrapper.cpp
#include "realLibrary.h" // Defines bar(int)
void libraryWrapper::bar(int x)
{
::bar(x)
}
Basic example

what are the paths that resolve the scope of the std library

So, I believe I understand the concept of the header method C++. But I read a post which become confusing. If I decide to avoid using directive in my programming I.E (using namespace std), how can I supplement other libraries that I may not be familiar with or where can I find a digital resource to help me resolve scope problems. I know that the "std" part in a std::cout statement resolves the the function from the iostream when I choice to include it. However, I am not getting the convention here. What would the scope parameter for be in a situation where I wanted to use the it but did not want to use the std namespace and how would this affect un-conventional classes?
Files which contain standart functions declarations - like iostream for example, are kept in directories depending on what ide you are using. For example, for Visual c++ studio 2008 these files are in [visual studio folder]/VC/include.
To find all files containing std namespace functions declarations you probably will need to search for all files containing _STD_BEGIN macro.

C++ multiple definition error when including external library

I'm trying to write a simple application allowing the user to perform a series of symbolic manipulations on a set of linear equations and am using the "Symbolicc++" library (more specifically, the latest version 3.35) for this purpose.
Since I don't have much experience with C++ and have never actually used a third-party library before, it's quite possible that I simply don't know how to properly use a library and am making some stupid mistake.
The problem is that I get a lot of multiple definition errors when I try to compile (and link) any program consisting of more than one file that includes the library's main header; the errors refer to functions and classes that are defined in the library's files (not mine).
A very simplistic example: suppose we have the files main.cpp, head.h and head.cpp. The contents is as follows:
main.cpp
------------------
#include <iostream>
#include "head.h"
int main()
{
return 0;
}
head.h
------------------
#ifndef SOMETHING
#define SOMETHING
#include "symbolicc++.h"
#endif
head.cpp
------------------
#include "head.h"
//nothing
Of course, the files in the real program contain a lot more, but even with just this, trying to build the program with, e.g.:
g++ -I /path to library's header files/ main.cpp head.cpp
yields hundreds of error message along the likes of:
/tmp/ccYNzlEF.o: In function `Cloning::Cloning()':
head.cpp:(.text+0x0): multiple definition of `Cloning::Cloning()'
/tmp/ccNWUnnC.o:main.cpp:(.text+0x0): first defined here
where, e.g., Cloning::Cloning() is declared in cloning.h, which is one of the library's header files.
A program containing only a single file including symbolicc++.h works just fine.
I also tried building this project on Visual Studio 2012 and got a similar result.
Unfortunately, I wasn't able to find any information about this problem, as virtually all the materials I found concerned errors in header files created by the user (as opposed to libraries created by someone else), so any help would be appreciated.
This library seems seriously broken. The way it is designed, you cannot include "symbolicc++.h" multiple times without violating the one-definition rule.
For example, let's have a look at cloning.h. It defines a Cloning class with a default constructor declaration:
class Cloning
{
private: int refcount;
void (*free_p)(Cloning*);
// ...
public: Cloning();
// ...
};
Later on, within the header file, it also defines that constructor:
Cloning::Cloning() : refcount(0), free_p(0) {}
That's it. Every one of your *.cpp files which directly or indirectly includes cloning.h will be exposed to the definition of the same function. The linker notices the multiple definitions and gives up.
Try to modify cloning.h. Remove the constructor definition line above and place the constructor definition into the class definition, making it an inline function:
// just to see what's going on...
class Cloning
{
private: int refcount;
void (*free_p)(Cloning*);
// ...
public: Cloning() : refcount(0), free_p(0) {};
// ...
};
This will fix the error for Cloning::Cloning. But that's just to shed more light on the issue; I don't advise you to do this. It would be the library authors' job to fix their code.
Further recommended reading: Why include guards do not prevent multiple function definitions?
Here is a way for you to reproduce the same problem with a few lines of your own code:
head.h:
#ifndef SOMETHING
#define SOMETHING
struct Example
{
Example(); // constructor declaration
};
Example::Example() // constructor definition
{
}
#endif
head.cpp:
#include "head.h"
//nothing
main.cpp:
#include "head.h"
int main()
{
}
Example linker error (taken from Visual C++ 2013):
1>main.obj : error LNK2005: "public: __thiscall Example::Example(void)" (??0Example##QAE#XZ) already defined in head.obj
1>[...] fatal error LNK1169: one or more multiply defined symbols found
If you absolutely must use this library, you'll have to build your own safe wrapper around it. But frankly, just don't. I'm sure there are numerous other libraries that solve the same problems and that can actually be used according to C++ language rules and conventions.

problem wrapping extern "C" library in a namespace

I am using a C library (libgretl) from C++ and some of its functions conflict with my code, so I wanted to wrap it in a namespace, like this:
namespace libgretl {
extern "C" {
#include <gretl/libgretl.h>
}}
However, this does not compile, I get "undefined" errors from gcc files (using mingw32 with gcc 4.5.2 on Windows).
The first errors come from the following code block of file c++/cstddef:
_GLIBCXX_BEGIN_NAMESPACE(std)
using ::ptrdiff_t;
using ::size_t;
_GLIBCXX_END_NAMESPACE
where the macros expand respectively to namespace std { and }. There are more errors after these.
Omitting the extern "C" directive does not help. Using an anonymous namespace reduces the amount of errors, but it still won't compile.
My question is therefore if there is some way to include such a C library and place its functions into a namespace, without changing the gcc or the library's source files?
Thanks.
Michal
You can't do it. Namespaces are not just source code decorations, they are mangled to object symbols by compiler.
Native C function foo() in library will be available by symbol _foo in object file, but calling bar::foo() will generate reference to, for example, #N3barfoo. As result, linker error will occur.
You may create "proxy" functions in separate source file, including original library header only in this source and putting all proxy functions to namespace.
You don't get to simply wrap a namespace around an external declaration and have it appear within that namespace... the item (function, global) must have been built within that namespace from the start. Since C doesn't support namespace resolution, this could not have been the case.
You need to change your own code to accommodate this library, unless you're willing to chante the library itself.
In order to refer to a non-namespace'd item that conflicts with your own namespace'd item, refer to ::item().
I guess the C library was compiled as C, which means namespaces are not included and not supported in the compiled code. Thus, your compiled C library cannot be in a namespace. Altering the header by encapsulating the include will not change that.
You can still encapsulate your own code in a namespace.