Boost tests in Visual Studio - c++

I am trying to compile trivial unit test project in Visual Studio 2010. I have a testrunner.cpp:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "BaumWelch Unit Tests"
#include <boost/test/unit_test.hpp>
and exampletests.cpp
#include <boost/test/unit_test.hpp>
int add( int i, int j ) { return i+j; }
BOOST_AUTO_TEST_CASE( my_test )
{
// seven ways to detect and report the same error:
BOOST_CHECK( add( 2,2 ) == 4 ); // #1 continues on error
BOOST_REQUIRE( add( 2,2 ) == 4 ); // #2 throws on error
if( add( 2,2 ) != 4 )
BOOST_ERROR( "Ouch..." ); // #3 continues on error
if( add( 2,2 ) != 4 )
BOOST_FAIL( "Ouch..." ); // #4 throws on error
if( add( 2,2 ) != 4 ) throw "Ouch..."; // #5 throws on error
BOOST_CHECK_MESSAGE( add( 2,2 ) == 4, // #6 continues on error
"add(..) result: " << add( 2,2 ) );
BOOST_CHECK_EQUAL( add( 2,2 ), 4 ); // #7 continues on error
}
This example worked fine in Linux, however here it fails to compile:
1>------ Build started: Project: Test, Configuration: Release Win32 ------
1>Build started 30/01/2013 14:47:48.
1>InitializeBuildStatus:
1> Touching "Release\Test.unsuccessfulbuild".
1>ClCompile:
1> All outputs are up-to-date.
1>boost_unit_test_framework-vc100-mt-1_46_1.lib(boost_unit_test_framework-vc100-mt-1_46_1.dll) : error LNK2005: "class boost::unit_test::master_test_suite_t & __cdecl boost::unit_test::framework::master_test_suite(void)" (?master_test_suite#framework#unit_test#boost##YAAAVmaster_test_suite_t#23#XZ) already defined in libboost_unit_test_framework-vc100-mt-1_46_1.lib(framework.obj)
1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain#16
1>C:\Users\ga1009\Documents\dev\Oasis\Release\Test.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
What am I doing wrong and how can I fix it?
EDIT: After applying the answer from Arne Mertz, I get:
1>------ Build started: Project: Test, Configuration: Release Win32 ------
1>Build started 30/01/2013 15:09:54.
1>InitializeBuildStatus:
1> Touching "Release\Test.unsuccessfulbuild".
1>ClCompile:
1> All outputs are up-to-date.
1>boost_unit_test_framework-vc100-mt-1_46_1.lib(boost_unit_test_framework-vc100-mt-1_46_1.dll) : error LNK2005: "class boost::unit_test::master_test_suite_t & __cdecl boost::unit_test::framework::master_test_suite(void)" (?master_test_suite#framework#unit_test#boost##YAAAVmaster_test_suite_t#23#XZ) already defined in libboost_unit_test_framework-vc100-mt-1_46_1.lib(framework.obj)
1>C:\Users\ga1009\Documents\dev\Oasis\Release\Test.exe : fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.

Check your project's linker properties: There is a property System/SubSystem that should be set to /SUBSYSTEM:CONSOLE.
Edit:
For the multiple definition error it seems you have the following situation:
In your testrunner.cpp, you define BOOST_TEST_DYN_LINK wich leads to a dynamic linking of boost_unit_test_framework-vc100-mt-1_46_1.dll which contains a definition of the master_test_suite function. In the other .cpp, you do NOT define that symbol, so that one gets linked statically against boost_unit_test_framework-vc100-mt-1_46_1.lib which, together with the dll gives two definitions.
Solution: use the #define in every source before including the header.

Related

Added opencv_world400.lib and opencv_world400d.lib to dependencies, still getting LNK2001 errors

I've added opencv_world400.lib and opencv_world400d.lib to the dependencies, but I'm still getting this error in MSVS2017:
1>------ Build started: Project: OpenCLTest, Configuration: Release x64 ------
1>OpenCLTest.obj : error LNK2001: unresolved external symbol "int __cdecl cv::_interlockedExchangeAdd(int *,int)" (?_interlockedExchangeAdd#cv##YAHPEAHH#Z)
1>c:\users\chubak\documents\visual studio 2017\Projects\OpenCLTest\x64\Release\OpenCLTest.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "OpenCLTest.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
There were no other lib files in the folder, just those two. What causes this problem I don't know.
Here's the code:
#include "stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat image = imread("C:\\Users\\Chubak\\Pictures\\index.jpg");
if (image.empty())
return -1;
imshow("TEST", image);
waitKey();
return 0;
}
3 steps:
1. C++ -> General -> Additional Include Directories
2. Linker -> Input -> Additional Dependencies
3. Linker -> General -> Additional Include Directories

Google Test and SFML

I want to use SFML and Google test for my project. I tried to set both up in DLL mode. I was able to start the program with SFML and it works, also google test works, at least until I try to test a class from my actual project.
I use VS2013 and have structured my project(s) like that.
If I try to use a class from the "Pacman" project it fails with the following:
1>------ Rebuild All started: Project: Pacman, Configuration: Release Win32 ------
1> Main.cpp
1> TestMe.cpp
1> Generating Code...
1> .NETFramework,Version=v4.0.AssemblyAttributes.cpp
1> LINK : /LTCG specified but no code generation required; remove /LTCG from the link command line to improve linker performance
1> Pacman.vcxproj -> C:\Users\xxxx\Documents\Git\pacman\Pacman\Release\Pacman.exe
2>------ Rebuild All started: Project: Pacman_Test, Configuration: Release Win32 ------
2> Pacman_Test.cpp
2>Pacman_Test.obj : error LNK2001: unresolved external symbol "public: int __thiscall TestMe::TestMyValue(int)" (?TestMyValue#TestMe##QAEHH#Z)
2>Pacman_Test.obj : error LNK2001: unresolved external symbol "public: __thiscall TestMe::TestMe(void)" (??0TestMe##QAE#XZ)
2>C:\Users\xxx\Documents\Git\pacman\Pacman\Release\Pacman_Test.exe : fatal error LNK1120: 2 unresolved externals
========== Rebuild All: 1 succeeded, 1 failed, 0 skipped ==========
Can anyone help?
EDIT how should it help to mark it as a duplicate? I can't see where there would be duplicate declarations in my project.
EDIT2 Here is the code for the TestMe class and yes it works in the main.cpp.
#pragma once
class TestMe
{
public:
TestMe();
int TestMyValue(int val);
};
#include "TestMe.h"
TestMe::TestMe(){}
int TestMe::TestMyValue(int val){
return 0;
}
To complete the code here is the main.cpp too:
#include <iostream>
#include <SFML/Graphics.hpp>
#include "TestMe.h"
int main()
{
TestMe testMe;
std::cout << "Value:" << testMe.TestMyValue(3) << std::endl ;
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}

Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt' on Visual Studio 2010 C ++? [duplicate]

This question already has answers here:
error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
(13 answers)
Closed 9 years ago.
I get the following error Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt' when I compile my code using Win32 Console Application.
I tried fixing it my going into Projects -> Properties -> General -> Linker -> Enable Incremental Linking and I changed it from "Yes" to No (/INCREMENTAL:NO) and then I tried to debug my code again but got another error message :
1>------ Build started: Project: Someproject, Configuration: Debug Win32 ------
1>project1.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
1>c:\users\anne\documents\visual studio 2010\Projects\Someproject\Debug\Someproject.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
How can I fix it?
#include <Windows.h>
#include <process.h>
#include <stdio.h>
#include <math.h>
volatile int counter = 0;
int isPrime(int n)
{
for(int i = 2; i < (int)(sqrt((float)n) + 1.0) ; i++) {
if (n % i == 0) return 0;
}
return 1;
}
unsigned int __stdcall mythread(void*)
{
char* s;
while (counter < 25) {
int number = counter++;
s = "No";
if(isPrime(number)) s = "Yes";
printf("Thread %d value = %d is prime = %s\n",
GetCurrentThreadId(), number, s);
}
return 0;
}
int main(int argc, char* argv[])
{
HANDLE myhandleA, myhandleB;
myhandleA = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);
myhandleB = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);
WaitForSingleObject(myhandleA, INFINITE);
WaitForSingleObject(myhandleB, INFINITE);
CloseHandle(myhandleA);
CloseHandle(myhandleB);
getchar();
system("pause");
return 0;
}
The basic problem is that you somehow specified "windows app" in your project settings. You want "console app" instead.
Windows apps use "WinMain()"; console apps use "main()".
Look at this link for details:
error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
See also:
Building Console Applications

Compiling Mathlink Code in Visual Studio 2010 Express LNK2019 Error

I'm attempting to compile a simple C file for use with Mathematica. (Note: I did follow the rest of the instructions, creating the empty addtwotm.c file and adding addtwo.tm)
#include "mathlink.h"
extern int addtwo( int i, int j);
int addtwo( int i, int j)
{
return i+j;
}
#if WINDOWS_MATHLINK
#if __BORLANDC__
#pragma argsused
#endif
int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow)
{
char buff[512];
char FAR * buff_start = buff;
char FAR * argv[32];
char FAR * FAR * argv_end = argv + 32;
hinstPrevious = hinstPrevious; /* suppress warning */
if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1;
MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
return MLMain( (int)(argv_end - argv), argv);
}
#else
int main(int argc, char* argv[])
{
return MLMain(argc, argv);
}
#endif
However, on build, I get this output:
1>------ Build started: Project: addtwo, Configuration: Debug Win32 ------
1> Performing Custom Build Tools
1> on "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\VC\bin\mprep.exe" "" -o "D:\Applications\Mathematica\SystemFiles\Links\MathLink\DeveloperKit\Windows\MathLinkExamples\addtwo\addtwo\..\addtwotm.c"
1>addtwo.obj : error LNK2019: unresolved external symbol _MLMain referenced in function _WinMain#16
1>addtwo.obj : error LNK2019: unresolved external symbol _MLInitializeIcon referenced in function _WinMain#16
1>D:\Applications\Mathematica\SystemFiles\Links\MathLink\DeveloperKit\Windows\MathLinkExamples\addtwo\Debug\addtwo.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I've followed all provided instructions from Wolfram's MathLink Developer Guide, and made sure to add "ml32i3m.lib" to Linker>Input>Additional Dependencies. Supposedly the ml32/ml64 lib files contain the information for MlMain. Any help is appreciated :)

error LNK2019: unresolved external symbol

Ok, so I'm having a problem trying figure out the problem in my code. I have a lot of code so I'm only going to post the relevant parts that are messing up when I compile. I have the following function inside of a class and it will compile and everything will run fine until I call the function "CalculateProbabilityResults" and it runs the 7th line of code within it. I've "de-commented" this line of code in my program so you can find it easier. I'm pretty sure I have the right #include directives needed since it compiles fine when not calling the function, so that can't be the problem can it? I know some of my naming notation needs a little help, so please bear with me. Thanks in advance for the help guys.
int SQLServer::CalculateProbabilityResults(int profile, int frame, int time_period, int TimeWindowSize) {
ofstream ResultFile;
stringstream searchFileName;
stringstream outputName;
vector<vector<int>> timeFrameItemsets;
int num = getTimeFrameFile(frame*TimeWindowSize, TimeWindowSize);
cout << num << endl;
//outputName << "Results" << getTimeFrameFile((frame*TimeWindowSize), TimeWindowSize) << ".csv";
cout << outputName.str() << endl;
outputName.clear();
//ResultFile.open(outputName.str().c_str());
ResultFile.close();
result.resize(0);
return 0;
}
int getTimeFrameFile(int timeInHours, int timeFrameSize) {
int fileNum = 0;
int testWin;
if (timeInHours > 24) {
while (timeInHours >24)
timeInHours -= 24;
}
for (testWin = 0; testWin < 24/timeFrameSize; testWin++) {
if (timeInHours >= testWin*timeFrameSize && timeInHours < (testWin+1)*timeFrameSize)
fileNum = testWin+1;
}
if (fileNum == 0)
fileNum = testWin+1;
return fileNum;
}
Call Log
1>------ Rebuild All started: Project: MobileSPADE_1.3, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'MobileSPADE_1.3', configuration 'Debug|Win32'
1>Compiling...
1>main.cpp
1>MobileSPADE.cpp
1>SQLServer.cpp
1>Generating Code...
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Linking...
1>LINK : C:\Users\JoshBradley\Desktop\MobileSPADE_1.3\MobileSPADE_1.3\Debug\MobileSPADE_1.3.exe not found or not built by the last incremental link; performing full link
1>SQLServer.obj : error LNK2019: unresolved external symbol "public: int __thiscall SQLServer::getTimeFrameFile(int,int)" (?getTimeFrameFile#SQLServer##QAEHHH#Z) referenced in function "public: int __thiscall SQLServer::CalculateProbabilityResults(int,int,int,int)" (?CalculateProbabilityResults#SQLServer##QAEHHHHH#Z)
1>C:\Users\JoshBradley\Desktop\MobileSPADE_1.3\MobileSPADE_1.3\Debug\MobileSPADE_1.3.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\JoshBradley\Desktop\MobileSPADE_1.3\MobileSPADE_1.3\MobileSPADE_1.3\Debug\BuildLog.htm"
1>MobileSPADE_1.3 - 2 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
The compiler thinks that getTimeFrameFile is a SQLServer method:
unresolved external symbol "public: int __thiscall SQLServer::getTimeFrameFile(int,int)"
but you have it defined as a free function:
int getTimeFrameFile(int timeInHours, int timeFrameSize) {
Change that from a free function to a class method will solve the problem:
int SQLServer::getTimeFrameFile(int timeInHours, int timeFrameSize)
Put the function getTimeFrameFile above SQLServer::CalculateProbabilityResults.