main() first defined here - c++

I'm trying to compile my project in Eclipse.
However, it says that the main() is defined more than once. I grep'd my project dir and it found only one definition of main(), in main.cpp.
Apparently it is somewhere else.maybe a dir I linked to.
The only dirs I linked to are:
-ljson_linux-gcc-4.5.2_libmt
The compiler output is:
make all
Building file: ../src/main.cpp
Invoking: GCC C++ Compiler
g++ -Ijson_linux-gcc-4.5.2_libmt -I/usr/include/mysql -I/usr/include/jsoncpp-src-0.5.0/include -O0 -g3 -Wall -c -fmessage-length=0 -Ijson_linux-gcc-4.5.2_libmt -MMD -MP -MF"src/main.d" -MT"src/main.d" -o"src/main.o" "../src/main.cpp"
Finished building: ../src/main.cpp
Building target: Atms
Invoking: GCC C++ Linker
g++ -L-L/usr/include/jsoncpp-src-0.5.0/include/ -o"Atms" ./src/atmstypes.o ./src/base64.o ./src/hregex.o ./src/libparser.o ./src/log.o ./src/main.o ./src/serv.o ./src/sqlfeeder.o ./src/teleindex.o ./src/telepipe.o ./src/telesharedobject.o ./src/treet.o ./src/ttable.o -l-ljson_linux-gcc-4.5.2_libmt
./src/serv.o: In function `main':
/usr/include/c++/4.4/new:101: multiple definition of `main'
./src/main.o:/home/idan/workspaceCpp/Atms/Debug/../src/main.cpp:12: first defined here
/usr/bin/ld: cannot find -l-ljson_linux-gcc-4.5.2_libmt
collect2: ld returned 1 exit status
make: *** [Atms] Error 1
main.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <iostream>
#include <string.h>
#include <string>
#include "../h/hregex.h"
using namespace std;
string s = "this and7 that";
int main(int argc,char** argv){
cout << hregex::InitRegex() << endl;
cout << hregex::CheckHostnameField(s)<< "= this and7 that" << endl;
s = "this and7 that";
cout << hregex::CheckURLField(s)<< "= this and7 that" << endl;
s = "/lol/idan.html";
cout << hregex::CheckURLField(s)<< "= /lol/idan.html" << endl;
s = "/lol2#/idan.html";
cout << hregex::CheckURLField(s)<< "= /lol2#/idan.html" << endl;
return 0;
}
How can I prevent the error from appearing?

g++ says serv.o has a main function.
If there actually is no main() it serv.cpp, check the includes, maybe you did a bad #include and included a .cpp instead of a .h ?
As an extra remark :
it tries to bind against the library "-ljson_linux-gcc-4.5.2_libmt"
So there is "-l-ljson_linux-gcc-4.5.2_libmt" in the link command line. Remove the -l in your configuration

Related

Eclipse issue linking sqlite3 library

Installed https://www.mingw-w64.org/ and added the libs but still getting this error:
Info: Internal Builder is used for build
g++ -std=c++11 -O3 -Wall -c -fmessage-length=0 -o main.o "..\\main.cpp"
g++ -std=c++11 "-LF:\\MinGW\\lib" -o test.exe main.o -lpsapi -lpthread -lm -ldl
main.o:main.cpp:(.text.startup+0x1f): undefined reference to `sqlite3_open'
main.o:main.cpp:(.text.startup+0x36): undefined reference to `sqlite3_errmsg'
f:/mingw/bin/../lib/gcc/i686-w64-mingw32/4.8.3/../../../../i686-w64-mingw32/bin/ld.exe: main.o: bad reloc address 0x36 in section `.text.startup'
collect2.exe: error: ld returned 1 exit status
Used the installer and choosed "Native Windows" + "i686".
The code:
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
#include <sqlite3.h>
sqlite3 *db;
int main() {
int open = sqlite3_open("test.db", &db);
if( open ) {
cerr << "Can't open database: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
exit(1);
}
return 0;
}

Linker cannot find local shared library

I'm trying a very simple exmaple to create a shared library and link to it. The shared library is as follows:
#ifndef ARDUGRAB_H_
#define ARDUGRAB_H_
#include <iostream>
using namespace std;
namespace ArduGrabLibrary{
class ArduGrab{
public:
ArduGrab();
virtual void initCamera();
virtual void setSim(bool sim);
virtual void setDebug(bool debug);
private:
bool debug = false;
bool sim = false;
};
}
Then the source code file is just as simple:
#include "ardugrab.h"
namespace ArduGrabLibrary
{
ArduGrab::ArduGrab(){
std::cout << "IMX298 Constructor" << std::endl;
}
void ArduGrab::initCamera(){
if (this->debug){
cout << "init camera" << std::endl;
}
}
void ArduGrab::setSim(bool sim){
this->sim = sim;
if (this->debug){
cout << "set sim to " << std::boolalpha << this->sim << std::endl;
}
}
void ArduGrab::setDebug(bool debug){
this->debug = debug;
if (this->debug){
cout << "set debug to " << std::boolalpha << this->sim << std::endl;
}
}
}
I'm then compiling that into a shared library with:
g++ -fPIC -shared -o ardugrab.so ardugrab.cpp
All good, we get an ardgrab.so library so to test it, with the following code in teh same directory as the .so and .h files from above:
#include "ardugrab.h"
using namespace ArduGrabLibrary;
int main() {
std::cout << "starting program" << std::endl;
ArduGrab* ardu = new ArduGrab();
ardu->setDebug(true);
//imx298->setSim(true);
//imx298->initCamera();
return 0;
}
So now we need to compile this into an executable with:
g++ -L. -lardugrab -o testardugrab testardugrab.cpp
This however fails to find the ardugrab.so file, the follow error message appears:
pi#raspberrypi:~/ArduMipiGrab $ g++ -L. -lardugrab -o testardugrab testardugrab.cpp
/usr/bin/ld: cannot find -lardugrab
collect2: error: ld returned 1 exit status
I've tried setting LD_LIBRARY_PATH to . export LD_LIBRARY_PATH=. but still nothing.
As you can see I'm a bit new with compiling c++, can someone please advise me as to what I'm doing wrong?
Thanks.
Reagrds,
Neil
This is becuase you are using the -l flag.
When you use this flag (Rather than specify a library specifically) it assumes a certain naming convention.
-lX
The linker assumes the file name is
libX.so (or libX.a)
So the commands you want are:
> g++ -fPIC -shared -o libardugrab.so ardugrab.cpp
> # ^^^
> g++ -L. -lardugrab -o testardugrab testardugrab.cpp
Note: The environment variable LD_LIBRARY_PATH is used at runtime when the standard library tries to find and load required shared libraries. I.E. it is not used during compilation to find shared libraries to link with.

C++ stoi not resolving using eclipse to compile

This code fails to compile with an error that it can't resolve stio. Have I made some newbie mistake here?
Eclipse Version: 3.8.1 Mint KDE should all be up to date.
GCC Version: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int main() {
string numberGuessed;
int intNumberGuessed = 0;
int answer = 0;
answer = (rand() % 100) + 1;
do {
cout << "Guess a number "; // prints !!!Hello World!!!
getline(cin, numberGuessed);
intNumberGuessed = stoi(numberGuessed);
cout << "You guessed "<< numberGuessed << endl;
cout << "You are not correct. Try again" << endl;
} while (answer != intNumberGuessed);
cout << "you got it";
return 0;
}
The error message.
16:39:14 **** Incremental Build of configuration Debug for project Hello2 ****
make all
Building file: ../src/Hello2.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Hello2.d" -
MT"src/Hello2.d" -o "src/Hello2.o" "../src/Hello2.cpp"
../src/Hello2.cpp: In function ‘int main()’:
../src/Hello2.cpp:27:40: error: ‘stoi’ was not declared in this scope
intNumberGuessed = stoi(numberGuessed);
^
make: *** [src/Hello2.o] Error 1
src/subdir.mk:18: recipe for target 'src/Hello2.o' failed
16:39:14 Build Finished (took 613ms)
The std::stoi function is available since the c++11 standard.
Apparently your compiler version of GCC is too old, to take c++11 as the current default standard.
You may try to specify the -std=c++11 or -std=c++0x compiler flags, or update your gcc compiler to one of the most recent versions.
Here's a link explaining in detail how to set the compiler flags.
This might help you with updating your compiler version to the latest.

C++: Armadillo does not want to cooperate with hdf5 format

Recently I have decided to store my data into hdf5 binary instead of ASCII files. I would like to use hdf5 format. Basically the thought is have the header and the data in the same file (header ASCII not binary format and then binary format). Something like this:
----------------------------------------
Dataname : testdata
ref_ell : wgs84
bmin :
etc.
and here are the data in hdf5 format
The armadillo library (http://arma.sourceforge.net/docs.html#save_load_mat) do have the function to append data to the existing file (hdf5_opts::append). But I have reached the problem much sooner. I have followed the manual but apparently I did something wrong. Lets say I have:
#include <iomanip>
#include <iostream>
#include <fstream>
#include <map>
#include <cmath>
#include <algorithm>
#include <vector>
#define ARMA_USE_HDF5
#include <hdf5.h>
#include <armadillo>
// g++ -O3 -lhdf5 -larmadillo -DARMA_DONT_USE_WRAPPER -DARMA_USE_BLAS -DARMA_USE_LAPACK -DARMA_USE_HDF5 - hdf5.cpp -o hdf5.o
// g++ -O3 -lhdf5 -larmadillo hdf5.cpp -o hdf5.o
// g++ -O3 -larmadillo -lhdf5 hdf5.cpp -o hdf5.o
using namespace std;
int main() {
arma::mat amat = arma::randu<arma::mat>(5,6);
cout << amat << endl;
amat.save( arma::hdf5_name("A.h5", "my_data"));
arma::mat bmat;
bool t = bmat.load( arma::hdf5_name("A.h5", "my_data"));
cout << bmat << endl;
if(t == false)
cout << "problem with loading" << endl;
return 0;
}
I tried to compile this exercise but I get only errors:
Either this:
hdf5.cpp: In function ‘int main()’:
hdf5.cpp:28:43: error: ‘hdf5_name’ was not declared in this scope
amat.save( hdf5_name("A.h5", "my_data"));
Or:
g++ -O3 -lhdf5 -larmadillo hdf5.cpp -o hdf5.o
hdf5.cpp: In function ‘int main()’:
hdf5.cpp:27:16: error: ‘hdf5_name’ is not a member of ‘arma’
amat.save( arma::hdf5_name("A.h5", "my_data"), arma::hdf5_binary);
What am I missing? (Solved - an update of the armadillo lib was required !)
Proceeding to second part of the problem: To save the header first and then add the data in hdf5 format. This way it works. But the header is added after the matrix is stored.
#include <iomanip>
#include <iostream>
#include <fstream>
#include <map>
#include <cmath>
#include <algorithm>
#include <vector>
#define ARMA_USE_HDF5
#define ARMA_DONT_USE_WRAPPER
#include <hdf5.h>
#include <armadillo>
// g++ -O3 -larmadillo -lhdf5 hdf5.cpp -o hdf5.o
using namespace std;
int main() {
arma::mat amat = arma::randu<arma::mat>(5,6);
cout << amat << endl;
amat.save( arma::hdf5_name("A.hdf5", "gmodel", arma::hdf5_opts::append ) );
ofstream f_out; f_out.open( "A.hdf5", ios::app );
f_out << "\nbegin_of_head ================================================\n";
f_out << "model name : " << "model_name" << endl;
f_out << "model type : " << "model_type" << endl;
f_out << "units : " << "units" << endl;
f_out << "ref_ell : " << "ref_ell" << endl;
f_out << "ISG format = " << "isg_format" << endl;;
f_out << "end_of_head ==================================================\n";
f_out.close();
return 0;
}
When i switch the order, the amat.save() function just rewrites the content of the A.hdf5 file.
For me the code worked (in Ubuntu 17.10) using
g++ hdf5.cpp `pkg-config --cflags --libs hdf5` -DARMA_DONT_USE_WRAPPER -I/home/claes/armadillo-8.500/include -o hdf5.o -lblas -llapack
where
`pkg-config --cflags --libs hdf5`
expands to
-I/usr/include/hdf5/serial -L/usr/lib/x86_64-linux-gnu/hdf5/serial -lhdf5

undefined reference to `__gcov_flush'

I am trying same,
http://www.linuxforums.org/forum/suse-linux/135465-gcov-g.html
Code from the link,
#include <iostream>
using namespace std;
void one(void);
void two(void);
void __gcov_flush(void);
int main(void)
{
int i;
while(true)
{
__gcov_flush();
cout << "Enter a number(1-2), 0 to exit " << endl;
cin >> i;
if ( i == 1 )
one();
else if ( i == 2 )
two();
else if ( i == 0 )
break;
else
continue;
}
return 0;
}
void one(void)
{ cout << "One is called" << endl; }
void two(void)
{ cout << "Two is called" << endl; }
but for me also it gives,
test.cpp:(.text+0x1d9): undefined reference to `__gcov_flush()'
collect2: ld returned 1 exit status
Tried the followings,
g++ -fprofile-arcs test.cpp
g++ -fprofile-arcs -g test.cpp
g++ -fprofile-arcs -ftest-coverage -g test.cpp
g++ -fprofile-arcs -ftest-coverage -g test.cpp -lgcov
I have also tried the "-lgcov" & "extern void __gcov_flush(void)" as mentioned in link above. I am currently on Ubuntu12.04 and g++ 4.6
So, I want to know if there is solution for this or gcov_flush doesnt work anymore.
void __gcov_flush();
Since the code is compiled as C++, this declares the existence of a C++ function of that name. C++ functions are subject to name mangling, so the (C++) symbol is not found in the (C) link library, and the linker (rightfully) complains about it.
If you declare the function, declare it as a function with C linkage:
extern "C" void __gcov_flush();
This should do the trick.
Note the commend by Paweł Bylica -- __gcov_flush() has been removed in GCC 11, you should use __gcov_dump().
I fixed this issue changing the settings.
Test Project --> Build Settings
Instrument Program Flow = Yes