PDA

View Full Version : XCode, C++ and Boost




XeDoSh
May 2, 2011, 09:57 AM
Hi guys out there,

since days I am trying to get one easy program started:

The *.cpp File:
/*
* TopologyPreserving.cpp
*/

#include "TopologyPreserving.h"

#include <iostream>

using namespace std;
using namespace boost::numeric::ublas;

namespace scils{ namespace algorithms{ namespace morphology{

TopologyPreserving::TopologyPreserving(ublas::matrix<double>& segmentedMatrix)
:segMask(segmentedMatrix.size1(),segmentedMatrix.size2()) {//const narrowBandWidth connectivity tau rho maxIter, const bool output)

// 1. Get mask of the segmented Matrix
//segMask = new ublas::matrix<double> (segmentedMatrix.size1(),segmentedMatrix.size2());

for (unsigned i = 0; i < segmentedMatrix.size1 (); ++ i) {
for (unsigned j = 0; j < segmentedMatrix.size2 (); ++ j) {
if (segmentedMatrix(i,j) != 0) {
segMask(i,j) = 1;
}
else {
segMask(i,j) = 0;
}
}
}
};
int main (int argc, char *argv[]) {
// Test example
// segmentedMatrix = [0 0 3 3;
// 0 0 0 3;
// 3 0 0 0;
// 3 3 0 0]
ublas::matrix<double> segmentedMatrix(4,4);
for (unsigned i=0; i<4; ++i) {
for (unsigned j=0; j<4; ++j) {
segmentedMatrix(i,j) = 0;
}
}
segmentedMatrix(0,2) = 3;
segmentedMatrix(0,3) = 3;
segmentedMatrix(1,3) = 3;
//
segmentedMatrix(2,0) = 3;
segmentedMatrix(3,0) = 3;
segmentedMatrix(3,1) = 3;

// Run algorithm (to get the mask from the segmented mask)
scils::algorithms::morphology::TopologyPreserving
myTP(segmentedMatrix);

cout << myTP.segMask << endl;

return 0;
}

}}}

And here is the header File:
/*
* TopologyPreserving.h
*/

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

namespace ublas = boost::numeric::ublas;

#include <stdio.h>
// #include <stdlib.>

namespace scils{ namespace algorithms{ namespace morphology{

class TopologyPreserving{
public:
/** Mask for segmented mask */
ublas::matrix<double> segMask;

/** Constructor */
TopologyPreserving(ublas::matrix<double>& segmentedMatrix);
};

}}}

I am trying to run that project using xCode and the well known boost library. But always, I am getting the following error message 3 times:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


I searched through the www for any help. But mostly some people gave the reason that the main() function is missing. But this is not the case here.

But I have found out that this is a so called "linker" problem. Unfortunately I do not understand how to deal with it.

So I really hope that someone here wants to be my hero for the whole week! :)

I would be really grateful for an answer :)!

Bye,
XeDoSh



chown33
May 2, 2011, 10:31 AM
In what namespace does your main() function reside?

In what namespace should it reside in order to be seen as the main entry-point for your program?

gnasher729
May 2, 2011, 12:55 PM
I searched through the www for any help. But mostly some people gave the reason that the main() function is missing. But this is not the case here.

First rule of debugging: _You_ made a mistake. Not the compiler, not the linker, it's _you_.

The linker tells you that the main function is missing. The correct approach would be to ask yourself "Why is the main function missing?" Instead you took the wrong approach of rejecting what the linker tells you. Ask yourself: "Why is this function that I _think_ is the main function not the main function"? And chown33 gave the answer to that.

lloyddean
May 2, 2011, 01:39 PM
A code reformatting tool might help make things a bit more obvious. With it you could temporarily change your code format and change back when done

Or even set it up to enforce something consistent to keep.


/*
* TopologyPreserving.h
*/

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

namespace ublas = boost::numeric::ublas;

#include <stdio.h>
// #include <stdlib.>

namespace scils
{
namespace algorithms
{
namespace morphology
{
class TopologyPreserving
{
public:
/** Mask for segmented mask */
ublas::matrix<double> segMask;

/** Constructor */
TopologyPreserving(ublas::matrix<double>& segmentedMatrix);
};
}
}
}






/*
* TopologyPreserving.cpp
*/

#include "TopologyPreserving.h"

#include <iostream>

using namespace std;
using namespace boost::numeric::ublas;

namespace scils
{
namespace algorithms
{
namespace morphology
{

TopologyPreserving::TopologyPreserving(ublas::matrix<double>& segmentedMatrix)
: segMask(segmentedMatrix.size1(), segmentedMatrix.size2())
// const narrowBandWidth connectivity tau rho maxIter, const bool output)
{

// 1. Get mask of the segmented Matrix
// segMask = new ublas::matrix<double> (segmentedMatrix.size1(),segmentedMatrix.size2());

for (unsigned i = 0; i < segmentedMatrix.size1 (); ++ i)
{
for (unsigned j = 0; j < segmentedMatrix.size2 (); ++ j)
{
if (segmentedMatrix(i, j) != 0)
{
segMask(i, j) = 1;
}
else
{
segMask(i, j) = 0;
}
}
}
};

int main (int argc, char *argv[])
{
// Test example
// segmentedMatrix = [0 0 3 3;
// 0 0 0 3;
// 3 0 0 0;
// 3 3 0 0]
ublas::matrix<double> segmentedMatrix(4, 4);

for (unsigned i = 0; i < 4; ++i)
{
for (unsigned j = 0; j < 4; ++j)
{
segmentedMatrix(i, j) = 0;
}
}

segmentedMatrix(0, 2) = 3;
segmentedMatrix(0, 3) = 3;
segmentedMatrix(1, 3) = 3;
//
segmentedMatrix(2, 0) = 3;
segmentedMatrix(3, 0) = 3;
segmentedMatrix(3, 1) = 3;

// Run algorithm (to get the mask from the segmented mask)
scils::algorithms::morphology::TopologyPreserving
myTP(segmentedMatrix);

cout << myTP.segMask << endl;

return 0;
}

}
}
}

XeDoSh
May 3, 2011, 02:55 AM
You are totally right. In fact, I did not really understand the way of using namespaces as one should. Thus, by trying to copy the style from another project where there is no main() function inside, this mistake arises. Anyway, I changed the namespace brackets _before_ the main() function and now, of course, it works :).

Thanks a lot! :)

XeDoSh