cppunit linking error - linker

When compiling my test suite againts cppunit, I get the following linking error:
error LNK2019: unresolved external symbol "public: virtual __thiscall CppUnit::TextTestRunner::~TextTestRunner(void)" (??1TextTestRunner#CppUnit##UAE#XZ) referenced in function _wmain
I don't understand this problem as the provided examples link fine.
I am using the macros to register my test:
CPPUNIT_TEST_SUITE_REGISTRATION
CPPUNIT_TEST_SUITE
etc
My test class is derived from
CPPUNIT_NS::TestFixture
and here is the 'main' code:
int _tmain(int argc, _TCHAR* argv[])
{
// Get the top level suite from the registry
CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
// Adds the test to the list of test to run
CPPUNIT_NS::TextUi::TestRunner runner;
runner.addTest( suite );
// Change the default outputter to a compiler error format outputter
runner.setOutputter( new CPPUNIT_NS::CompilerOutputter( &runner.result(),
CPPUNIT_NS::stdCOut() ) );
// Run the test.
bool wasSucessful = runner.run();
// Return error code 1 if the one of test failed.
return wasSucessful ? 0 : 1;
}

Are you certain that you have added the appropriate libraries to the Linker -> Input -> Additional Dependencies text box for your project? I believe it should be (for the Release / Unicode version):
testrunneru.lib cppunit.lib
You also need to be sure the linker can find the library by adding the appropriate directory name to Linker -> General -> Additional Library Directories.
A real stretch, but it happened to me once, was I needed to rebuild cppunit. I think my compiler was upgraded or patched.
It might also be helpful if you could post a very short, but complete, source file that reproduces the problem. I think you are only missing the include statements here?

Related

'Undefined Reference' in unit test project

I am using TinyEmbeddedTest and VisualGDB in Visual Studio 2015, and attempting to create a unit test project as part of my solution. My main project builds and runs fine, but the unit test project is getting linker errors and will not build.
The tests are in a source file, ADCTests.cpp, which has
#include “ADC.c”
Which I believe is appropriate for TinyEmbeddedTest.
The file ADC.c contains the following include lines:
#include “ADC.h”
#include “fault.h”
The errors are thrown in ADC.c, on the following statements:
if (status == SUCCESS)
{
Fault_Clear(FAULT_ADC_FAILURE);
rawADCValue = HAL_ADC_GetValue(hADC);
}
else
{
Fault_Set(FAULT_ADC_FAILURE);
rawADCValue = 0u;
}
The errors are:
undefined reference to ‘Fault_Clear’
undefined reference to ‘Fault_Set’
From this I can see that the header file, fault.h, where these functions are declared, is accessible to the toolchain, because otherwise there would be a compilation error, but the source file. fault.c, where these functions are defined, does not seem to be accessible. Both of these files are in the same project.
I do not get these errors when running my main project, only in the unit test project.
I thought that perhaps the answer might lie in the project linker settings, and so I added a line to the Linker->[Additional Inputs] as follows:
C:\Development\myProject\VisualGDB\Debug\Src
I assumed that this would give the linker access to the .o files, created by the compiler. Unfortunately this created a new problem. The build now fails without any error messages. The list of build messages simply says
Build failed: arm-none-eabi-g++.exe exited with code 1 Id returned 1
exit status
and when I look at the log, it says
c:/sysgcc/arm-eabi/bin/../lib/gcc/arm-none-eabi/9.3.1/../../../../arm-none-eabi/bin/ld.exe:
cannot find C:/Development/myProject/VisualGDB/Debug/Src: Permission
denied
Interestingly, that last path is a hyperlink, and when I click on it, I get an error dialog box
Failed to open
‘C:\SysGCC\arm-eabi\Development\myProject\VisualGDB\Debug\Src’
This is especially odd, because I know that path does not exist, and that is definitely not the path that I entered in the Linker settings.
Any guidance would be much appreciated.

Linking error while customizing zcalloc and zcfree functions in the zlib library

While integrating the Nurbs library (http://www.rhino3d.com/opennurbs) into my project, I run into the following linking error with the library zlib.
1>zlib.lib(deflate.obj) : error LNK2019: unresolved external symbol _zcfree referenced in function _z_deflateInit2_
1>zlib.lib(inflate.obj) : error LNK2001: unresolved external symbol _zcfree
1>zlib.lib(deflate.obj) : error LNK2019: unresolved external symbol _zcalloc referenced in function _z_deflateInit2_
1>zlib.lib(inflate.obj) : error LNK2001: unresolved external symbol _zcalloc
I checked the function
z_deflateInit2
in the file deflate.c from ZLIB source code,
and guess that this function cannot find the implementation of the two functions zcfree adn zcalloc. The reason is that the Nurbs library customize the function zcfree and zcalloc, which is done in two steps.
Cusomize zcalloc and zcfree in the the Nurbs library code.
opennurbs_zlib.h
extern "C" {
voidpf zcalloc (voidpf, unsigned, unsigned);
void zcfree (voidpf, voidpf);
}
These two functions are implementated in the file opennurbs_zlib_memory.cpp as following
#define voidpf z_voidpf
voidpf zcalloc (voidpf, unsigned items, unsigned size)
{
return oncalloc(items, size);
}
void zcfree (voidpf, voidpf ptr)
{
onfree(ptr);
}
Compile the ZLIB library with the flag: MY_ZCALLOC and Z_PREFIX
As I checked, both these two steps are done, but why I still got the linking error.
Could you give me some advices?
Thanks so much!
Update:
#Dale Lear: thanks for your support. But my situation is different. Instead if linking with the opennurbs.lib, I tried to integrate the source code of opennurbs into my project (like the surface module of point cloud library: http://www.pointclouds.org/blog/trcs/moerwald/). I build zlib from the project zlib in opennurbs solution. This zLib is built with the modified zconfig.h (Z_PREFIX and Z_MYCALL, i guess so, is defined). This means that the function zfree and zalloc is still waiting for implementation. But why does ZLib does not take zcfree() and zcalloc from opennurbs_zlib_memory.cpp. I don't understand why does it take.
From your description and the error log you provided, I'm guessing that the situation is:
1) You are building a Windows program using some version of Microsoft's C++ compiler.
2) You want to statically link with zlib.lib
3) You want to statically link with opennnurbs_staticlib.lib
I cannot determine what version of Microsoft's C++ compiler or opennurbs you're using.
If you are using the latest public release of opennurbs (version 2013-07-11), then the zcfree() and zcalloc() functions are defined in the file opennurbs_zlib_memory.cpp.
If you build opennurbs_staticlib.lib using the opennurbs_staticlib.vcxproj project file that is included with the source code, it will compile opennurbs_zlib_memory.cpp and include the zcfree() and zcalloc() functions in it in opennurbs_staticlib.lib. If you build zlib using the zlib code and zlib/zlib.vcxproj file that is included with opennurbs 2013-07-11, then it will be built with all the necessary defines and you will have two static libraries, zlib.lib and opennurbs_staticlib.lib, that link with all dependencies resolved.
If you are using customized project files, the first thing to check is that you are statically linking the results of compiling opennurbs_zlib_memory.cpp in some way.
If you want to use opennurbs as a DLL, I'd suggest building opennurbs.dll with the opennurbs.vcxproj project file that comes with the source code. This opennurbs.dll will statically include zlib.lib when it links and you do not have to link with anything except the resulting opennurbs.lib to use the DLL version.
Does this help?
-- Dale Lear

How to link a dll, to an exe file in run-time?

I am new to programming and I created a dll project in which, i will just print a line. In the application project I have called the function defined in the dll project.
My question is, I am getting the dll file, soon after building the dll project. But when I build the main app, i.e., application project i am getting the following error.
--------------------Configuration: test_bench - Win32 Debug--------------------
Compiling...
main.c
Linking...
main.obj : error LNK2001: unresolved external symbol _print_dll
../../exec/test_bench.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
test_bench.exe - 2 error(s), 0 warning(s)
If I link the obj before the build, it gets built. But if I change the code of dll project, again i have to re-build the main project, which should not be necessary while running a dll.
Please help me to achieve this
I wrote a little binary template to help with dlopen related calls. You'd need to tweak it for your specific use case (as it is, it only handles strings), but I have found it handy on many occasions.
Usage: dlopener /path/to/library.extension function_name [args...]
//gcc -rdynamic -o dlopener dlopener.c -ldl
#include <dlfcn.h> /*for dlopen,dlsym,dlclose*/
int main(int argc, char **argv){
/* get a "handle" for a shared library*/
void *handle = dlopen(argv[1], RTLD_LAZY);
/* make sure we got a handle before continuing*/
if (! handle) return 1;
/*undefined, but workable solution : POSIX.1-2003 (Technical Corrigendum 1) */
void* (*f)()=dlsym(handle, argv[2]);
/*now call the function f(argv[3],argv[4],...argv[argc]); */
//TODO convert args to unsigned char representations for other types
while (argc > 2) /*ugh, have to use asm to preserve stack*/
asm("push %0"::"r"(argv[argc--])); /*from right to left*/
asm("call *%0"::"r"(f)); //TODO "=a"(ret) where is uchar[XXX]
/*remember that shared library we opened?*/
dlclose(handle);
return 0;
}
Note: for some WIN32 functions (cdecl calling convention IIRC) you need to use a different asm mechanism than push to get the values in registers instead of on the stack
This error indicates that you didn't link the library (DLL) in your executable, that's why it can't find the function print_dll.
You can load libraries dynamically in Windows through LoadLibray()
Here is a sample on how you do it: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686944(v=vs.85).aspx
For linking the dll project in run time, you need to implement the run time linking in your main file. If you implement the the run time linking means, no need to re-build the project every time.
For run time linking you need the following functions
#include <dlfcn.h>
void *dlopen(const char *filename, int flag);
char *dlerror(void);
void *dlsym(void *handle, const char *symbol); //symbol is your function name
int dlclose(void *handle);
Link with -ldl.
See the man page for more details.
but while changing your code, if you change the function name you should use the same name in the place of symbol.

adding extra (pthread) dll in visual studio 2010 [duplicate]

Just to be clear - I have searched the depths of the internet and back for information on how to do this
I'm looking for assistance setting up pthread_Win32 to work with Visual Studio 2005. I'm programming in C, and I have a number of multithreaded assignments to write using pthread.h. However, since pthread is native to unix, I have to write all of my code, ftp it, and then ssh to my class' remote unix system to run it. It makes development take so much longer, and it's incredibly inefficient. I'd love (more than anything) to be able to get this working on my win32 machine, so I can develop in visual studio as I've been doing for quite some time.
I've installed the pthread.lib file and pthread.h file into the respective lib/header directories, where all of the other files are. The DLL on the other hand (the actual library), I've placed in c:\windows\system32. I've tried to add the DLL as a dependency (right click project -> references -> Add new reference), but as others have stated, all I get is a blank dialogue box with no option to add any DLL files or anything. It seems to recognize the header file, but I get these errors when I compile:
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol _imp_pthread_join referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _imp_pthread_create referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _imp_pthread_exit referenced in function _fcount
From my research, I've determined that this is a problem with the DLL, and I'm assuming it can't find the definitions of the functions I've referenced in my code. I've searched high and low and I can't seem to figure out any way to overcome this problem. I've added the directories of the lib/header files to my linker, just in-case, but that didn't solve the issue. I need to do something, in visual studio, to specify that I need pthreadVC2.dll as a project dependency - and it seems to be impossible (and extremely frustrating) at this point.
Any words of wisdom?
Thank you very much
I have been through this problem recently.
It appears that the __imp__ prefix is prepended to the function name in case pthread.h is included in dynamic linking mode.
Simply add the PTW32_STATIC_LIB define to your project or your source code before including pthread.h should solve the issue.
#define PTW32_STATIC_LIB
#include <pthread.h>
Although, I am not completely over as Visual Studio now trys to link with the _[FuncName] instead of [FuncName]
In visual studio, function seems to be declared differently wether you are going to link them statically (.lib) or dynamically (.dll).
To define a function you will link dynamically :
__declspec (dllimport) int myFunc(int myArgs) ;
To define function you are exporting for dynamic linking :
__declspec (dllexport) int myFunc(int myArgs) ;
And the simpliest, to define a function you will link statically:
int myFunc(int myArgs) ;
[EDIT]
I am going on my investigations and went through this on MS help center.
It seems that to avoid the _[FuncName] effect it is required to define a static linked library function by the following:
int __cdecl myFunc(int MyArgs) ;
Have you added pthreadVC.lib (or whichever particular lib you need) to the project property:
Linker/Input/Additional Dependencies
It's not enough to just have the lib file in a particular directory, the linker needs to be told to use it as an input.
Just adding pthreadVC2.lib to linker list is not suffiecient.
You also have to add addtional lib like pthreadVCE2.lib and pthreadVSE2.lib.
I am facing same issue but then I resolved it through adding these files.

How do I install pthread_win32 (Windows pthread / posix thread library) for Visual Studio 2005?

Just to be clear - I have searched the depths of the internet and back for information on how to do this
I'm looking for assistance setting up pthread_Win32 to work with Visual Studio 2005. I'm programming in C, and I have a number of multithreaded assignments to write using pthread.h. However, since pthread is native to unix, I have to write all of my code, ftp it, and then ssh to my class' remote unix system to run it. It makes development take so much longer, and it's incredibly inefficient. I'd love (more than anything) to be able to get this working on my win32 machine, so I can develop in visual studio as I've been doing for quite some time.
I've installed the pthread.lib file and pthread.h file into the respective lib/header directories, where all of the other files are. The DLL on the other hand (the actual library), I've placed in c:\windows\system32. I've tried to add the DLL as a dependency (right click project -> references -> Add new reference), but as others have stated, all I get is a blank dialogue box with no option to add any DLL files or anything. It seems to recognize the header file, but I get these errors when I compile:
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol _imp_pthread_join referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _imp_pthread_create referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _imp_pthread_exit referenced in function _fcount
From my research, I've determined that this is a problem with the DLL, and I'm assuming it can't find the definitions of the functions I've referenced in my code. I've searched high and low and I can't seem to figure out any way to overcome this problem. I've added the directories of the lib/header files to my linker, just in-case, but that didn't solve the issue. I need to do something, in visual studio, to specify that I need pthreadVC2.dll as a project dependency - and it seems to be impossible (and extremely frustrating) at this point.
Any words of wisdom?
Thank you very much
I have been through this problem recently.
It appears that the __imp__ prefix is prepended to the function name in case pthread.h is included in dynamic linking mode.
Simply add the PTW32_STATIC_LIB define to your project or your source code before including pthread.h should solve the issue.
#define PTW32_STATIC_LIB
#include <pthread.h>
Although, I am not completely over as Visual Studio now trys to link with the _[FuncName] instead of [FuncName]
In visual studio, function seems to be declared differently wether you are going to link them statically (.lib) or dynamically (.dll).
To define a function you will link dynamically :
__declspec (dllimport) int myFunc(int myArgs) ;
To define function you are exporting for dynamic linking :
__declspec (dllexport) int myFunc(int myArgs) ;
And the simpliest, to define a function you will link statically:
int myFunc(int myArgs) ;
[EDIT]
I am going on my investigations and went through this on MS help center.
It seems that to avoid the _[FuncName] effect it is required to define a static linked library function by the following:
int __cdecl myFunc(int MyArgs) ;
Have you added pthreadVC.lib (or whichever particular lib you need) to the project property:
Linker/Input/Additional Dependencies
It's not enough to just have the lib file in a particular directory, the linker needs to be told to use it as an input.
Just adding pthreadVC2.lib to linker list is not suffiecient.
You also have to add addtional lib like pthreadVCE2.lib and pthreadVSE2.lib.
I am facing same issue but then I resolved it through adding these files.

Resources