Boost.Test - Undefined symbol when overriding main - c++

I want to provide my own main function while using Boost.Test. So I have included the following macros:
#define BOOST_TEST_ALTERNATIVE_INIT_API
#define BOOST_TEST_NO_MAIN
My main function looks like this:
int main(int argc, char* argv[])
{
int exitCode = ::boost::unit_test::unit_test_main(&initialise, argc, argv);
return exitCode;
}
I have also created the function initialise.
When built on OS X using Xcode 6 the following error is reported:
Undefined symbols for architecture x86_64:
"boost::unit_test::unit_test_main(bool (*)(), int, char**)", referenced from:
_main in main.o
I am linking-in the unit test framework library.
Can someone please help resolve the error?

There are two possible solutions:
1) Include <boost/test/included/unit_test.hpp> in the test module. This has the effect of building both the test framework and the test module simultaneously.
2) Rebuild the test framework library withBOOST_TEST_NO_MAINand BOOST_TEST_ALTERNATIVE_INIT_API.
According to the documentation the 2nd option is preferred.

Related

Unresolved External _main in a console application

I have the following main function:
int main(int argv, char** argc) {
MainGame mainGame;
mainGame.run();
system("pause");
return 0;
}
it throw a LNK2019 unresolved _main at me.
now i did a bit of google-ing and found countless examples of people having accidentlyt setup win32 applications instead of the intended console app, so I checked mine in linker->system->subsystem and it read console.
Had a similar problem when using SDL. I've added #undef main after #include "SDL.h" since main was defined inside SDL for some other purpose (as DimChtz pointed out in the comments). It solved the problem.
By the way, this does not have to be SDL-specific. Some other included header or source file in your project could also be #define-ing "main", which would trigger this behavior.

GoogleTest C++ - Test Fixture

I'm a beginner in C++ and am coding a Snake for uni, and have to run unit tests.
FYI, I code on Xcode 7.1.1.
I manage to make sample tests run on my machine, but have a problem when it comes to creating a fixture for my snake. Here is the code I have :
#include "gtest/gtest.h"
#include "calc.h"
#include "Serpent.h"
#include "Map.h"
#include "Main.h"
using namespace std;
using namespace sf;
class Serpent_test_fixture: public ::testing::Test
{public:
Serpent* serpent_test;
Map* map_test;
Serpent_test_fixture(){
serpent_test = new Serpent(true);
RenderWindow window(VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32), "SnakeTest", Style::None);
map_test = new Map("", window, false);
}
virtual void SetUp(){
map_test->updateGameField(0, 0, HEAD_EAST);
serpent_test->setHead(*map_test, false);
int size_init = serpent_test->getSize();
}
virtual void TearDown(){
}
~Serpent_test_fixture(){
delete serpent_test;
delete map_test;
}
};
TEST_F(Serpent_test_fixture, cherry_action)
{
map_test->updateGameField(0,0,CHERRY);
Tiles head_tile_test = HEAD_EAST;
serpent_test->fruit_action(*map_test, head_tile_test);
EXPECT_EQ(20, 20);
}
int main(int argc, char * argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
The way I understand it is, I create my snake (serpent in French) from my class Serpent, then I create my map from my class Map.
UpdateGameField updates the tile in (0,0) of the map and puts "HEAD_EAST" on it.
Anyway, here is the message I have :
Undefined symbols for architecture x86_64:
"Map::updateGameField(int, int, Tiles)", referenced from:
Serpent_test_fixture_test_cherry_action_Test::TestBody() in main.o
Serpent_test_fixture::SetUp() in main.o
"Map::Map(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, sf::RenderWindow const&, bool)", referenced from:
Serpent_test_fixture::Serpent_test_fixture() in main.o
"Map::~Map()", referenced from:
Serpent_test_fixture::SetUp() in main.o
Serpent_test_fixture::~Serpent_test_fixture() in main.o
"Serpent::fruit_action(Map&, Tiles&)", referenced from:
Serpent_test_fixture_test_cherry_action_Test::TestBody() in main.o
"Serpent::getSize()", referenced from:
Serpent_test_fixture::SetUp() in main.o
"Serpent::setHead(Map, bool)", referenced from:
Serpent_test_fixture::SetUp() in main.o
"Serpent::Serpent(bool)", referenced from:
Serpent_test_fixture::Serpent_test_fixture() in main.o
"Serpent::~Serpent()", referenced from:
Serpent_test_fixture::~Serpent_test_fixture() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Can anyone help? It's my first time using Google tests and I'm having a hard time making it work.
The basic issue here is how to orchestrate the dependencies for a test project relative to the production code to be tested and the test framework. The dependency relationships drive the build system (compiler, linker, etc.).
You might want to take a look at my C++ Now! 2014 presentation on Test-Driven Development in C++. It uses Boost.Test instead of google test, but shows a CMake based recipe to orchestrate the dependencies between production code and test code. The workshop is designed for you to follow along at your computer, replicating the steps in the presentation -- I provide the code you use at each step.
The dependencies look like this for a typical testing project:
test executable
production code to be tested (static library or shared library)
test framework
In my workshop materials I show how to use CMake to create these dependencies using Boost.Test as the test framework, but the principle is the same with google test and the recipe is nearly identical as well.

Compiling C++ with SDL on Mac OS X Lion

I thought Ubuntu and OS X would have similar interfaces for compiling a C/C++ program with SDL but nothing I am trying, or finding on Google seems to be working.
I have found a solution for Xcode, but I am not using Xcode. I am writing my programs from Sublime Text 2 and compiling via command-line since I prefer it and work much faster that way.
Long story short, I am receiving an array of errors with each attempt and thus far I have copied SDL.framework into my /Library/Frameworks directory.
This is the closest I have gotten to actually compiling:
[ 674 / 174 / 0 ] $ compcpsdl
Undefined symbols for architecture x86_64:
"_SDL_main", referenced from:
-[SDLMain applicationDidFinishLaunching:] in ccYYA0Ea.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Terminal command (i.e. compcpsdl)
g++ -I/Library/Frameworks/SDL.framework/Headers main.cpp SDLmain.m -framework SDL -framework Cocoa
With the following project structure:
Project-Directory/
--- main.cpp
--- SDLMain.m
--- SDLMain.h
And lastly, the following source code:
#include <iostream>
#include <SDL/SDL.h>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
It appears I'm doing everything correctly, yet nothing seems to be working.
Any ideas guys?
I Think SDL does sometimes a bit strange stuff with the main function it assumes the mainfunction is defined in one way. Notice that I added int argc and char** argv to the definition of you main function.
Try:
int main (int argc, char** argv)
{
cout << "Hello World!";
return 0;
}
I encountered this problem also once and it was quite mystifying.
for more info see this stackoverflow question: Why SDL defines main macro?

SDL Error Undefined symbols for architecture x86_64 "_SDL_main"

I am using C++ with the SDL Cocoa and Foundation framework on my mac os x. I get the following error
Undefined symbols for architecture x86_64:
"_SDL_main", referenced from:
-[SDLMain applicationDidFinishLaunching:] in SDLMain.o
ld: symbol(s) not found for architecture x86_64
when I run the following code
#import <Foundation/Foundation.h>
#import <SDL/SDL.h>
#include "SDLMain.h"
int main(int argc, const char * argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640,480,32,SDL_DOUBLEBUF);
SDL_Event event;
bool isRunning = true;
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
isRunning=false;
}
}
SDL_Quit();
return 0;
}
I have no idea what is wrong, although it seems that when I go into the SDLMain.m file and comment out this line of code
status = SDL_main (gArgc, gArgv);
the program compiles with no problems. However, it doesn't work. No window opens like its supposed to. Any ideas?
I bet your main function signature is incorrect. You use:
int main(int argc, const char * argv[])
^^^^^
but SDL_main.h wants
int main(int argc, char *argv[])
Why?
You see, SDL does something really horrific when compiling: It renames your main function to SDL_main, injecting its own main function which, in turn, calls yours.
Note that if this doesn't work, then you may be compiling with wrong flags. To be sure, get the flags by typing:
$ sdl-config --cflags --libs
For more information, see Simply including SDL header causes linker error
I had the same problem, even though my main signature was correct. I Found the answer here: SDL in XCode 4.3.2 SDLMain.o undefined symbols
Turned out the problem was that I added SDLMain.m and SDLMain.h
(according to a suggestion on the libSDL website about OS X
frameworks) to an existing SDL project which messed up with main.
Bottom line is that you don't need those files just because you are
using Cocoa -- SDL's own test apps don't use it either and run just
fine on OS X.

Undefined Symbol: _sf_open (Simple audio stuff on OSX)

I'm trying to get into C++ programming, and I'm quite struggling against the little details. Right now, I'm trying to get the snippet below to work, and apparently it will compile, but not link. (error message is a the bottom of this post)
I'm using libsndfile to open audio files, and the linker doesn't seem to find the corrent library files for it. This is on OS X 10.5.
I've downloaded libsndfile from mega-nerd.com and installed it via the ususal configure, make, sudo make install procedure. Is there anything else I need to do?
Edit: I have macports installed on my system, if that's of any concern.
Here's my code:
#include <iostream>
#include <string>
#include <sndfile.h>
#include "stereoSplit.h"
using namespace std;
int stereoSplit(string filename)
{
cout << filename;
SF_INFO sfinfo;
sfinfo.format = 0;
SNDFILE * soundfile;
soundfile = sf_open( filename.c_str(), SFM_READ, &sfinfo );
return 0;
}
int main (int argc, char const *argv[])
{
return stereoSplit("testStereo.wav");
}
And here's the complete error message:
Undefined symbols:
"_sf_open", referenced from:
stereoSplit(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in cc3hvkkk.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
You need to link in the library. Your compiler command should look something like:
g++ mystuff.cpp -lsndfile
To link the library in Xcode, select the project target, add a new item to Link binary with libraries, and find the libsndfile.1.dynlib on your computer.