Building and using Log4cplus with CMake - c++

community,
I have the following problem with building and using the Log4cplus library with Embarcadero.
First, I download the library from http://sourceforge.net/p/log4cplus/wiki/Home/, then I navigate to a Directory where I want to build my library, and type cmake -G "Borland Makefiles" mypathtotherootomypreviousdownload. It then says, the build files have been written to the current directory.
Now I start the MSYS sh.exe and type make. This does also work. I get a bin Folder with various working tests (as exe files) of the library. In this bin folder there is also a dll log4cplusUD.dll. Now I also find in a src folder the corresponding LIB log4cplusUD.lib.
I am of course familiar how you can statically link a dynamic library with Embarcadero. But on attempting to compile
#pragma hdrstop
#pragma argsused
#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif
#include <stdio.h>
#include "log4cplus/logger.h"
#include "log4cplus/consoleappender.h"
#include "log4cplus/loglevel.h"
#include <log4cplus/loggingmacros.h>
#include <iomanip>
using namespace std;
using namespace log4cplus;
int _tmain(int argc, _TCHAR* argv[])
{
log4cplus::initialize ();
SharedAppenderPtr append_1(new ConsoleAppender());
append_1->setName(LOG4CPLUS_TEXT("First"));
Logger::getRoot().addAppender(append_1);
Logger root = Logger::getRoot();
Logger test = Logger::getInstance(LOG4CPLUS_TEXT("test"));
LOG4CPLUS_DEBUG(root,
"This is"
<< " a reall"
<< "y long message." << endl
<< "Just testing it out" << endl
<< "What do you think?");
test.setLogLevel(NOT_SET_LOG_LEVEL);
LOG4CPLUS_DEBUG(test, "This is a bool: " << true);
LOG4CPLUS_INFO(test, "This is a char: " << 'x');
LOG4CPLUS_INFO(test, "This is a short: " << static_cast<short>(-100));
LOG4CPLUS_INFO(test, "This is a unsigned short: "
<< static_cast<unsigned short>(100));
LOG4CPLUS_INFO(test, "This is a int: " << 1000);
LOG4CPLUS_INFO(test, "This is a unsigned int: " << 1000u);
LOG4CPLUS_INFO(test, "This is a long(hex): " << hex << 100000000l);
LOG4CPLUS_INFO(test, "This is a unsigned long: " << 100000000ul);
LOG4CPLUS_WARN(test, "This is a float: " << 1.2345f);
LOG4CPLUS_ERROR(test,
"This is a double: "
<< setprecision(15)
<< 1.2345234234);
LOG4CPLUS_FATAL(test,
"This is a long double: "
<< setprecision(15)
<< 123452342342.25L);
LOG4CPLUS_WARN(test, "The following message is empty:");
LOG4CPLUS_WARN(test, "");
return 0;
}
I get a linker error,
not resolved external Symbol log4cplus::Logger::getInstance.
The code above is just one of the tests that compiled and run well, when running make from sh.exe.
So what am I doing wrong?
I also tried to link the above main.cpp againts log4cplusUD.lib with the following CMakeLists.txt file
cmake_minimum_required (VERSION 2.6)
project (log4cplustest)
include_directories("C:/Users/fin/Software/log4cplus-1.2.0-rc3/log4cplus-1.2.0-rc3/include")
add_executable(log4cplustest main.cpp)
target_link_libraries(log4cplustest log4cplusUD)
but I get the same Linker error!

Related

What causes the following linking error with the boost c++ libraries?

Hi I get a linking error when compiling my program with the gcc compiler on cygwin. The first picture is a simple sample program from the boost filesystem libraries tutorial page where I have included filesystem.hpp in the boost folder. Beneath that is the picture of my linker error when I try to compile with the following command:
g++ -I C:/Users/Ejer/Desktop/c++Dep/boost_1_77_0 -I C:/Users/Ejer/Desktop/c++Dep/eigen-3.4.0 -L C:/Users/Ejer/Desktop/c++Dep/boost_1_77_0/stage/lib test.cpp -o ser
Here I try to compile my program test.cpp with the eigen and boost libraries and set the includer path that they tell me to set as the path after I have built the library with b2.exe. I have also linked to the lib files for boost. I have also tried linking to the different filesystem lib files specifically. Thanks in advance
#include <iostream>
#include <boost/filesystem.hpp>
using std::cout;
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: tut3 path\n";
return 1;
}
path p (argv[1]);
try
{
if (exists(p))
{
if (is_regular_file(p))
cout << p << " size is " << file_size(p) << '\n';
else if (is_directory(p))
{
cout << p << " is a directory containing:\n";
for (directory_entry& x : directory_iterator(p))
cout << " " << x.path() << '\n';
}
else
cout << p << " exists, but is not a regular file or directory\n";
}
else
cout << p << " does not exist\n";
}
catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}
return 0;
}
I get a linking error when compiling my program
No, you don't. You are getting a linking error when linking your program, not when compiling it.
The reason: you didn't supply the library (-L C:/Users/.... tells the linker where to search for libraries; not which libraries to link). Your command line should look something like:
g++ -I ... -L ... test1.cpp -o ser -lboost_filesystem

res->getString(1) mysql connector crash debug

I have a problem when i use mysql connector c++ and visual studio on the debug mode. It compiles just fine. My algorithm works on release mode. When i switch to debug mode, he complies but he crash while running.
I believe that he crash each time i use res->getString() .
I m using visual studio 2013, mysql connector.C++ 1.1 . I already added the dependencies
C:\Program Files\MySQL\Connector.C++ 1.1\lib\debug
C:\Program Files\Boost SDK;
C:\Program Files\MySQL\Connector.C++ 1.1\include
mysqlcppconn.lib and mysqlcppconn.dll
CPPCONN_PUBLIC_FUNC= ;HAVE_INT8_T=1 ( on the processor definition)
run time library : /MDd ( it the only think that works )
I copied mysqlcppconn.lib and mysqlcppconn.dll in the debug directory of my project
with this configuration my programm works on release mode ( changing Connector.C++ 1.1\lib\debug to Connector.C++ 1.1\lib\opt ).
In the debug mode, he says Debug Assertion failed :
File: f:\dd\vctools\crt\crtw32\misc\dbgheap.c
Line 1322
Expression : _CrtIsValidHeapPointer( pUserData)
Press Rety to debug the application
then i had to choose one buttom ( Ignore; Restart ; leave ). When i press Ignore he continue to run i have the first results but he crashs again
This is my code section
{
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <memory>
#include <string>
/* MySQL Connector/C++ specific headers */
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/metadata.h>
#include <cppconn/resultset_metadata.h>
#include <cppconn/exception.h>
#include <cppconn/warning.h>
#include "mysql_connection.h"
using namespace std;
using namespace sql;
int main(int argc, const char **argv)
{
const char* str_DataBaseMarketData_Server = "root";
const char* str_DataBaseMarketData_User = "root";
const char* str_DataBaseMarketData_Password ="root";
const char* str_DataBaseMarketData_Database ="bd test";
cout << "Connector/C++ tutorial framework..." << endl;
cout << endl;
string fld;
try {
sql::Driver* driver = get_driver_instance();
std::auto_ptr<sql::Connection> con(driver->connect(str_DataBaseMarketData_Server, str_DataBaseMarketData_User, str_DataBaseMarketData_Password));
con->setSchema(str_DataBaseMarketData_Database);
std::auto_ptr<sql::Statement> stmt(con->createStatement());
sql::SQLString *data = new SQLString();
std::auto_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT * FROM news "));
//ResultSetMetaData *res_meta = res->getMetaData();
//cout << res_meta->getColumnDisplaySize( 1) << endl << endl;
while (res->next()){
fld = res->getString(2).c_str();
}
}
catch (sql::SQLException &e) {
/*
The MySQL Connector/C++ throws three different exceptions:
- sql::MethodNotImplementedException (derived from sql::SQLException)
- sql::InvalidArgumentException (derived from sql::SQLException)
- sql::SQLException (derived from std::runtime_error)
*/
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
/* Use what() (derived from std::runtime_error) to fetch the error message */
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
return EXIT_FAILURE;
}
cout << "Done." << endl;
return EXIT_SUCCESS;
}
}
This things turns me crazy because when i replace getString() by anything else, it works sweetly.Could you help me please ?
I m running under a 32 bit.

mkdir() returns -1 when launched through Finder

I have a simple program that makes a directory when it is executed:
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(){
if(int a = mkdir("abc",0700)){
std::cout << "Failed to create: " << a << std::endl;
}
else{
std::cout << "Created." << std::endl;
}
}
It behaves differently for two different use cases:
Running the compiled binary through Terminal
Output: Created.
Launching this program via Finder with double click.
Output: Failed to create: -1
How do I make this so that launching this program via Finder creates the folder abc without using Cocoa framework (compiles with g++ only)?
Thanks to Wooble for pointing it out in the comment section that the problem is due to the working directory. When I launched it through Finder, the current working directory was my home directory.
Below is how I address the problem:
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libproc.h>
int main(int argc, char** argv){
// Gets and prints out the current directory
char cwd[1024];
getcwd(cwd, sizeof(cwd));
std::cout << "Current Working Directory: " << cwd << std::endl;
// Above is not necessary
// Changes working directory to directory that contains executable
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
if(proc_pidpath (getpid(), pathbuf, sizeof(pathbuf)) > 0){ // Gets the executable path
std::string s(pathbuf);
s = s.substr(0,s.find_last_of('/')); // Removes executable name from path
std::cout << "Executable Path: " << s << std::endl;
chdir(s.c_str()); // Changes working directory
}
if(int a = mkdir("abc",0700)){
std::cout << "Failed to create: " << a << std::endl;
}
else{
std::cout << "Created." << std::endl;
}
}

Check GCC version using Eclipse

I try to get my gcc version using below code.
when I build and run it ,
console show "Nothing to build fro (my project name) ".
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main( int argc, char ** argv ) {
stringstream version;
version << "GCC version: "
<< __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__
<< "\nVersion string: " << __VERSION__;
cout << version.str() << endl;
return 0;
}
I found the solution.
I didn't set path variable to MinGW.
I it working properly.
Thank you all.

log4cpp gives `unresolved external` errors

I'm trying to make work the library and run the tests provided with the latest version of log4cpp on Borland Codegear 2007, in which it's included a bpr project for Borland C++ Builder 5, which is meant to be able to build and run the different tests. The problem is that I'm trying to open this project with the 2007 version, which has to carry out a project conversion. I was getting weird 'unresolved external' errors. Then I've tried to build the project myself without converting anything, but got stuck in the same point.
I'm trying to run the following test :
#include <stdio.h>
#include "log4cpp/Portability.hh"
#ifdef LOG4CPP_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <iostream>
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#ifdef LOG4CPP_HAVE_SYSLOG
#include "log4cpp/SyslogAppender.hh"
#endif
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/NDC.hh"
int main(int argc, char** argv) {
log4cpp::Appender* appender;
#ifdef LOG4CPP_HAVE_SYSLOG
log4cpp::SyslogAppender* syslogAppender;
syslogAppender = new log4cpp::SyslogAppender("syslog", "log4cpp");
#else
log4cpp::Appender* syslogAppender;
syslogAppender = new log4cpp::OstreamAppender("syslogdummy", &std::cout);
#endif
if (argc < 2) {
appender = new log4cpp::OstreamAppender("default", &std::cout);
} else {
appender = new log4cpp::FileAppender("default", argv[1]);
}
syslogAppender->setLayout(new log4cpp::BasicLayout());
appender->setLayout(new log4cpp::BasicLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
root.addAppender(syslogAppender);
root.setPriority(log4cpp::Priority::ERROR);
log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
sub1.addAppender(appender);
log4cpp::Category& sub2 = log4cpp::Category::getInstance(std::string("sub1.sub2"));
log4cpp::NDC::push(std::string("ndc1"));
std::cout << " root prio = " << root.getPriority() << std::endl;
std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;
root.error("root error");
root.warn("root warn");
sub1.error("sub1 error");
sub1.warn("sub1 warn");
sub2.error("sub2 error");
sub2.warn("sub2 warn");
sub1.setPriority(log4cpp::Priority::INFO);
std::cout << " root prio = " << root.getPriority() << std::endl;
std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;
std::cout << "priority info" << std::endl;
root.error("root error");
root.warn("root warn");
sub1.error("sub1 error");
sub1.warn("sub1 warn");
sub2.error("sub2 error");
sub2.warn("sub2 warn");
sub2.warnStream() << "streamed warn";
sub2 << log4cpp::Priority::WARN << "warn2" << " warn3"
<< log4cpp::eol << " warn4";
{
for(int i = 0; i < 10000; i++) {
char ndc2[20];
sprintf(ndc2, "i=%d", i);
log4cpp::NDC::push(ndc2);
sub1.info("%s%d", "i = ", i);
if ((i % 10) == 0) {
sub1.log(log4cpp::Priority::NOTICE, "reopen log");
if (log4cpp::Appender::reopenAll()) {
sub1.info("log reopened");
} else {
sub1.warn("could not reopen log");
}
}
#ifndef WIN32
sleep(1);
#endif
log4cpp::NDC::pop();
}
}
return 0;
}
The errors are all about 'unresolved external', such as:
[ILINK32 Error] Error: Unresolved external 'log4cpp::Category::warn(const char *, ...)' referenced from C:\DOCUMENTS AND SETTINGS\MLERMA\MIS DOCUMENTOS\RAD STUDIO\PROJECTS\DEBUG\TESTMAIN.OBJ
I'm getting this kind of error for every single call to a log4cpp function, all referring to TESTMAIN.OBJ .
Any ideas on this? is there anyone out there who has worked with log4cpp on Borland ?
Are you linking in the log4cpp library (.LIB)? I'm not familiar with BCB5 or Codegear but check your link libraries in your project settings and make sure the log4cpp library is included.
If it is, then you might have a problem with where the compiler is looking for libraries. Usually an IDE will have a project level or global setting that says where there compiler will look for .lib files. Check that setting and make sure one of those directories includes your log4cpp .lib file.
Mike
Try adding #pragma comment(lib,"ws2_32.lib") do your header file.