LNK2019 on function that does exist in an included *.cpp file - visual-studio

For Visual Studio 2019 on Windows 10 in a new project I'm getting an unexpected LNK2019 error.
I have a test function calling a function in another project where I've included the necessary *.cpp file into the test project so I can access the internals of a DLL for testing.
test.cpp:
#include "dllsubmodule.h"
void TestCountPagesInSomething()
{
// ... setup ...
int nPages;
nPages = CountPagesInSomething(iCurrentPos, hashFoundPages, hashPageCounts, sFoundData);
}
int main(int argc, char **argv)
{
TestCountPagesInSomething();
}
dllsubmodule.h
#pragma once
int CountPagesInSomething(
int iCurrentPos,
std::map<int, ERPPageInfo>& hashFoundPages,
std::map<std::string, int>& hashPageCounts,
const wxString& sFoundData
);
dllsubmodule.cpp
#include "stdafx.h"
#include "dllsubmodule.h"
int CountPagesInSomething(
int iCurrentPos,
std::map<int, ERPPageInfo>& hashFoundPages,
std::map<std::string, int>& hashPageCounts,
const wxString& sFoundData
)
{
// implementation code here...
}
For whatever reason the linker error seems to be complaining that CountPagesInSomething() doesn't exist. I suspect I'm having trouble with name mangling variations but I've no idea how to diagnose the problem.
Note: CountPagesInSomething() is internal to the DLL and not a DLL export.
Right now this is just a console project. I'm trying to get a basic printf() test working before I figure out what kind of unit test framework to use.

Related

how to fix undefined symbol for achitecture x86_64 error

This is my main code:
#include "startup.h"
int main(int argc, char *argv[]) {
printBanner(); <-- Undefined symbol for x86_64
readParameters(argc, argv); <-- Undefined symbol for x86_64
}
Contents for startup.h:
#ifndef STARTUP_H
#define STARTUP_H
#if defined __cplusplus
extern "C" {
#endif
void printBanner(void);
void readParameters(const int argc, char *argv[]);
#if defined _cplusplus
}
#endif
#endif
The implementations of the above mentioned functions is in startup.c:
#include "startup.h"
void readParameters(const int argc, char *argv[]) {
// code
}
void printBanner() {
// code
}
All the 3 files are within the same folder. Is there anything that is missing in terms of settings? I'm using xcode for this project.
I'm calling a function that is defined in .h from my main and the header is included in the .c file where functions are implemented. Can't see anything wrong with it.

Cereal: how to manage the implementation struct for the PIMPL idiom

I am having troubles to use cereal with the PIMPL idiom.
This is a minimal example:
b.h
#ifndef _B_H_
#define _B_H_
#include <memory>
#include "cereal/types/memory.hpp"
#include "cereal/archives/json.hpp"
struct BImpl;
class B
{
public:
B();
~B();
private:
std::unique_ptr<BImpl> _impl;
friend class cereal::access;
template <class Archive>
void serialize( Archive& ar )
{
ar( CEREAL_NVP( _impl ) );
}
};
#endif
b.cpp
#include "b.h"
struct BImpl
{
int b_i = 0;
private:
friend class cereal::access;
template <class Archive>
void serialize( Archive & ar )
{
ar(
CEREAL_NVP( b_i )
);
}
};
B::B() : _impl( new BImpl )
{
}
B::~B()
{
}
main.cpp
#include "b.h"
#include <fstream>
#include "cereal/archives/json.hpp"
using namespace std;
int main( int argc, char** argv )
{
B b1;
{
std::ofstream file( "out.json" );
cereal::JSONOutputArchive archive( file );
archive( CEREAL_NVP( b1 ) );
}
}
And here the errors that I get on MSVC 2015 Community Edition when I try to compile the minimal example:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(428): error C2139: 'BImpl': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_polymorphic'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(435): error C2139: 'BImpl': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_abstract'
I am quite sure that I am not the first attempting to do this, but I have not been able to find nothing specific in the documentation or code snippets with a working solution.
I have found a working solution following the approach described here: http://www.boost.org/doc/libs/1_46_1/libs/serialization/doc/pimpl.html
In practice, I have:
* moved the definition of B::serialize to B.cpp
* added in B.cpp all the different instantiations for the archives that I use
Here the ticket that describe the issue: https://github.com/USCiLab/cereal/issues/324

wxWidgets event table with custom event

I'm trying to implement a custom event in my wxWidgets application but I can't write the event table macros in a proper way.
the files that I use to implement the event is like the following:
the .h file
#ifndef __APP_FRAME_H__
#define __APP_FRAME_H__
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#include <wx/evtloop.h>
#include "wxApp.h"
#include "sampleCefApp.h"
class appFrame: public wxFrame
{
public:
appFrame(const wxString &title, const wxPoint &pos, const wxSize &size);
private:
int OnExit();
void OnCefStartEvent(wxCommandEvent &e);
DECLARE_EVENT_TABLE()
};
#endif
the .cpp file
// File : appFrame.cpp
#include "appFrame.h"
wxDEFINE_EVENT(CEF_START_EVT, wxCommandEvent)
void appFrame::OnCefStartEvent(wxCommandEvent &e)
{
CefRunMessageLoop();
}
int appFrame::OnExit(){
CefShutdown();
Destroy();
return 0;
}
appFrame::appFrame(const wxString &title, const wxPoint &pos, const wxSize &size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
}
wxBEGIN_EVENT_TABLE(appFrame, wxFrame)
EVT_COMMAND(wxID_ANY, CEF_START_EVT, appFrame::OnCefStartEvent)
wxEND_EVENT_TABLE()
And when I build my make file I get the following errors:
../src/appFrame.cpp:4:15: error: expected constructor, destructor, or type conversion before ‘(’ token
../src/appFrame.cpp:24:2: error: expected constructor, destructor, or type conversion before ‘wxEventTableEntry
I think the problem is related to mis-placing event table macros.
I want to know what is the problem exactly and how to fix it ?
You need a semicolon after wxDEFINE_EVENT() macro (as for almost all macros with wx prefix, they consistently require a semicolon, unlike the legacy macros without the prefix).
As usual, see the sample for the example of use of this macro.

Boost serialization polymorphic register(export) not working across files

I am using boost::serialization in my project. The project is large, and serializes my objects in several places. According to the documentation here, I should export my class with two separated step.
BOOST_EXPORT_KEY() in .h file, witch contains the declaration.
BOOST_EXPOET_IMPLEMENT() in .cpp file, witch contains the instantiation(definition) of the exporting.
hier.h the class hierarchy, there are 3 classes in the hierarchy.
/*
B <---+--- D1
|
+--- D2
*/
#include <boost/serialization/base_object.hpp>
class B {
public:
virtual ~B() {}
template < typename Ar >
void serialize(Ar& ar, const int) {
}
} ;
class D1 : public B {
public:
virtual ~D1() {}
template < typename Ar > void serialize(Ar& ar, const int) {
boost::serialization::base_object<B>(*this);
}
} ;
class D2 : public B {
public:
template < typename Ar > void serialize(Ar& ar, const int) {
boost::serialization::base_object<B>(*this);
}
virtual ~D2() {}
} ;
#include <boost/serialization/export.hpp>
BOOST_CLASS_EXPORT_KEY(B);
BOOST_CLASS_EXPORT_KEY(D1);
BOOST_CLASS_EXPORT_KEY(D2);
And a hier.cpp contains the implementation:
#include <boost/serialization/export.hpp>
#include "hier.h"
BOOST_CLASS_EXPORT_IMPLEMENT(D1);
BOOST_CLASS_EXPORT_IMPLEMENT(D2);
And a main.cpp use the serialization:
#include <iostream>
#include <sstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/export.hpp>
#include "hier.h"
int main(int argc, char* argv[])
{
B* d1 = new D1();
B* d2 = new D2();
std::ostringstream os;
boost::archive::text_oarchive oa (os);
oa & d1 & d2;
}
It compiled without any problem, but run it will cause:
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): unregistered class - derived class not registered or exported
Which means the derived class is not registered, means the registration in the hier.cpp is not working. But that is really strange, because:
If I register implementation is both main.cpp and hier.cpp, it issue duplicated definition while linking. Means the registration in hier.cpp is OK and is exposed into the linkers visibility., otherwise there will be no duplicated definition error.
If I register implementation only in main.cpp, it runs OK.
I am really confused in that situation. Any comment and suggestion is appreciated. Thanks in advance.
Before calling BOOST_CLASS_EXPORT_* you should include the archives which you want to use. The maсro then adds specific serialize-functions for the headers.
This means you should change your code in hier.cpp to the following:
#include <boost/serialization/export.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "hier.h"
BOOST_CLASS_EXPORT_IMPLEMENT(D1);
BOOST_CLASS_EXPORT_IMPLEMENT(D2);
The code in hier.h changes accordingly:
#include <boost/serialization/export.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
BOOST_CLASS_EXPORT_KEY(B);
BOOST_CLASS_EXPORT_KEY(D1);
BOOST_CLASS_EXPORT_KEY(D2);
Sources:
Boost Serialization Documentation
PS:
I do not know if this is solving your problem, but I think it could be causing some trouble. I think it's worth a try.

How to use pthread library in DevC++?

I downloaded pthread package from pthread. What should I do now to use it in DevC++?
Download pthreads devpak Download
Install it in Dev C++
Create new Project in Dev C++
After that go to Project menu -> Project Option -> In that select "Parameter Tab"
Select "add Library or object" option
Select "libpthreadGC2.a" file from installation directory of Dev c++
It will be in LIB directory.
Press ok
Now test following sample code ready for running..
Sample Code :
#include <iostream>
#include <pthread.h>
using namespace std;
void * fun_thread1(void *data)
{
for(int i=0;i<100;i++)
{
cout<<endl<<"In Thread 1"<<endl;
}
}
void * fun_thread2(void *data)
{
for(int i=0;i<100;i++)
{
cout<<endl<<"In Thread 2"<<endl;
}
}
int main(int argc, char *argv[])
{
int status;
// creating thread objects
pthread_t thrd_1;
pthread_t thrd_2;
// create thread
pthread_create(&thrd_1,NULL,fun_thread1,(void *)0);
pthread_create(&thrd_2,NULL,fun_thread2,(void *)0);
pthread_join(thrd_1, (void **)&status);
pthread_join(thrd_2, (void **)&status);
system("PAUSE");
return EXIT_SUCCESS;
}

Resources