0
I am trying to use a library in my C++ project with CMAKE, the problem that the library is loading the System class, and not my class that has the same name only the namespace that changes. It is as follows:
-ROOT
main.cpp
DataBase.cpp
DataBase.h // Este está com o #include "String.h"
includes
String
includes
String.h
RsaString.h
lib
libString.a
libRsaString.a
mysql
includes
windows
linux
macos
sqlite
CMakeList.txt
My Cmake file is as follows:
cmake_minimum_required(VERSION 3.4)
project(DataBase)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
include_directories(include/mysql/includes)
include_directories(include/sqlite)
include_directories(include/String/includes)
set(SOURCE_FILES main.cpp DataBase.cpp DataBase.h)
add_executable(DataBase ${SOURCE_FILES})
IF (WIN32)
#WINDOWS
target_link_libraries(DataBase ${CMAKE_SOURCE_DIR}/include/Mysql/windows/mysqlcppconn-static.lib)
ELSE()
IF(APPLE)
#MAC OS
target_link_libraries(DataBase ${CMAKE_SOURCE_DIR}/include/String/lib/libString.a)
target_link_libraries(DataBase ${CMAKE_SOURCE_DIR}/include/Mysql/macos/libmysqlcppconn-static.a)
ELSE()
#LINUX
target_link_libraries(DataBase ${CMAKE_SOURCE_DIR}/include/Mysql/linux/libmysqlcppconn-static.a)
ENDIF()
ENDIF()
Issue 1
Error:
/Applications/Programas/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/pedrosoares/Library/Caches/CLion2016.1/cmake/generated/DataBase-38151ccf/38151ccf/Debug --target all -- -j 4
[ 33%] Building CXX object CMakeFiles/DataBase.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/DataBase.dir/DataBase.cpp.o
In file included from /Users/pedrosoares/ClionProjects/DataBase/DataBase.cpp:5:
In file included from /Users/pedrosoares/ClionProjects/DataBase/DataBase.h:9:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:436:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstring:61:
In file included from /Users/pedrosoares/ClionProjects/DataBase/include/String/includes/string.h:9:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628:
In file included from /Users/pedrosoares/ClionProjects/DataBase/main.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:436:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstring:61:
In file included from /Users/pedrosoares/ClionProjects/DataBase/include/String/includes/string.h:9:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1565:17: error: no member named 'memcpy' in namespace 'std::__1'; did you mean 'wmemcpy'?
_VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
If I comment the include_directories line(include/String/includes), I get the following error:
/Applications/Programas/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/pedrosoares/Library/Caches/CLion2016.1/cmake/generated/DataBase-38151ccf/38151ccf/Debug --target all -- -j 4
Scanning dependencies of target DataBase
[ 33%] Building CXX object CMakeFiles/DataBase.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/DataBase.dir/DataBase.cpp.o
In file included from /Users/pedrosoares/ClionProjects/DataBase/DataBase.cpp:5:
/Users/pedrosoares/ClionProjects/DataBase/DataBase.h:62:17: error: unknown type name 'vector'
virtual vector< map<std::string, Tigre::String*> > query(std::string query);
^
/Users/pedrosoares/ClionProjects/DataBase/DataBase.h:62:23: error: expected member name or ';' after declaration specifiers
virtual vector< map<std::string, Tigre::String*> > query(std::string query);
~~~~~~~~~~~~~~^
2 errors generated.
make[2]: *** [CMakeFiles/DataBase.dir/DataBase.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
In file included from /Users/pedrosoares/ClionProjects/DataBase/main.cpp:2:
/Users/pedrosoares/ClionProjects/DataBase/DataBase.h:62:17: error: unknown type name 'vector'
virtual vector< map<std::string, Tigre::String*> > query(std::string query);
^
/Users/pedrosoares/ClionProjects/DataBase/DataBase.h:62:23: error: expected member name or ';' after declaration specifiers
virtual vector< map<std::string, Tigre::String*> > query(std::string query);
~~~~~~~~~~~~~~^
2 errors generated.
make[2]: *** [CMakeFiles/DataBase.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/DataBase.dir/all] Error 2
make: *** [all] Error 2
But if I besides comment the line and put the complete PATH in the class include it compiles perfectly.
/Applications/Programas/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/pedrosoares/Library/Caches/CLion2016.1/cmake/generated/DataBase-38151ccf/38151ccf/Debug --target all -- -j 4
Scanning dependencies of target DataBase
[ 33%] Building CXX object CMakeFiles/DataBase.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/DataBase.dir/DataBase.cpp.o
[100%] Linking CXX executable DataBase
[100%] Built target DataBase
Class so compiles only without include_directories
:
//
// Created by Pedro Soares on 4/24/16.
//
#ifndef DATABASE_DATABASE_H
#define DATABASE_DATABASE_H
#include "exception"
#include <string>
#include "map"
#include "include/String/includes/String.h"
namespace Tigre {
using namespace std;
class DataBaseException : public std::exception {
public:
/** Constructor (C strings).
* @param message C-style string error message.
* The string contents are copied upon construction.
* Hence, responsibility for deleting the \c char* lies
* with the caller.
*/
explicit DataBaseException(const char* message):
msg_(message)
{
}
/** Constructor (C++ STL strings).
* @param message The error message.
*/
explicit DataBaseException(const std::string& message):
msg_(message)
{}
/** Destructor.
* Virtual to allow for subclassing.
*/
virtual ~DataBaseException() throw (){}
/** Returns a pointer to the (constant) error description.
* @return A pointer to a \c const \c char*. The underlying memory
* is in posession of the \c Exception object. Callers \a must
* not attempt to free the memory.
*/
virtual const char* what() const throw (){
return msg_.c_str();
}
protected:
/** Error message.
*/
std::string msg_;
};
class DataBase {
public:
DataBase();
~DataBase();
virtual vector< map<std::string, Tigre::String*> > query(std::string query);
virtual bool execute(std::string query);
virtual void close();
protected:
virtual bool open(std::string filename);
virtual bool open();
};
}
#endif //DATABASE_DATABASE_H
And so do not compile with and without the include_directories
//
// Created by Pedro Soares on 4/24/16.
//
#ifndef DATABASE_DATABASE_H
#define DATABASE_DATABASE_H
#include "exception"
#include <string>
#include "map"
#include "String.h"
namespace Tigre {
using namespace std;
class DataBaseException : public std::exception {
public:
/** Constructor (C strings).
* @param message C-style string error message.
* The string contents are copied upon construction.
* Hence, responsibility for deleting the \c char* lies
* with the caller.
*/
explicit DataBaseException(const char* message):
msg_(message)
{
}
/** Constructor (C++ STL strings).
* @param message The error message.
*/
explicit DataBaseException(const std::string& message):
msg_(message)
{}
/** Destructor.
* Virtual to allow for subclassing.
*/
virtual ~DataBaseException() throw (){}
/** Returns a pointer to the (constant) error description.
* @return A pointer to a \c const \c char*. The underlying memory
* is in posession of the \c Exception object. Callers \a must
* not attempt to free the memory.
*/
virtual const char* what() const throw (){
return msg_.c_str();
}
protected:
/** Error message.
*/
std::string msg_;
};
class DataBase {
public:
DataBase();
~DataBase();
virtual vector< map<std::string, Tigre::String*> > query(std::string query);
virtual bool execute(std::string query);
virtual void close();
protected:
virtual bool open(std::string filename);
virtual bool open();
};
}
#endif //DATABASE_DATABASE_H
My problem is that I don’t think it’s good to practice for the full path in the library include, right? What would be the best way to solve this? So I read on the internet the correct is to use the 'include_directories' but it generates error.
What exactly do you mean by "system class"? I don’t understand what the error is. It would be a linkediting failure (didn’t find the file
libString.a
, for example)?– Luiz Vieira
Gives build error because it loads the system string and not lib. If you want I can cause the build error.
– Pedro Soares
What error? Edit the question and post the error message the compiler gives you. Also, I insist: what is "system string"? What system is this? Perhaps you mean that it confuses with the string class of STL? I think it is good to also put the code snippet where you use your string class, to check if the use of namespace is correct.
– Luiz Vieira