Eclipse C++ add shared library to main project - c++

I´m using Eclipse 3.8.1 on Ubuntu 14.02 with 2 projects for the first time. I´m coming from c# world so that can be an Eclipse error or a C++ concept error.
testmonitor: A sample C++ project. Code:
#include <iostream>
using namespace std;
int main() {
cout << "Test program" << endl;
log_access::test();
return 0;
}
log_access is a shared library: log_access.cpp
#include <iostream>
namespace log_access {
void test()
{
std::cout << "It worked!!!" << std::endl;
}
}
I´m trying to build a shared library and link it to the main project. I went to Project -> Properties -> Project References and clicked on the project (shared lib) I want to reference.
Not worked....
Then I went to Project -> Properties -> C/C++ General -> Paths and Symbols -> References Tab and clicked on the project (shared lib) I want to reference.
Not worked...
Currently I´m getting the following error:
Invoking: GCC C++ Compiler
g++ -std:c++0x -I"home/projects/dev/sample/workspace/log_access" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/testproject.d" -MT"src/testmonitor.d" -o "src/testmonitor.o" "../src/testmonitor.cpp"
../src/testmonitor.cpp: In function 'int main()':
../src/testmonitor.cpp:34.3: error: 'log_access' has not been declared
log_access:test();
^
make: *** [src/testmonitor.o] Error 1
13:56:39 Build Finished (took 1s.246ms)
Obs: The log_access compiles fine...
I appreciate very much some help on that...

You'll need to include your definition of log_access::test in your main file via
#include "log_access.h"
Assuming you have a header file named log_access (you shouldn't include .cpp files; use them for implementing methods declared within the header file. See here for why).

Related

Simple project won't compile

I'm still having issues with an ongoing project that just won't compile. I've narrowed it down to the Includes but can't figure out what is going on. I've read that i need to add a WinMain entry point but that doesnt add up - I have classmates that didnt encounter this shit error at all.
So I've created a new empty project:
#include <cstdlib> //include c library
//using namespace std;
//using namespace cv;
namespace sp {
int main() {
return 0;
}
}
With the following includes:
Under GCC C++ Compiler Includes:
C:\Users\Amit\Desktop\opencv\build\include
C:\opencv_contrib-3.0.0\modules\xfeatures2d\include
Under MinGW C++ Linker Libraries:
libopencv_core310
libopencv_imgcodecs310
libopencv_imgproc310
libopencv_xfeatures2d310
libopencv_features2d310
libopencv_highgui310
Under MinGW C++ Linker Library search path:
C:\Users\Amit\Desktop\opencv\build\x86\mingw\lib
Still, without calling any function from those libraries, I'm getting this error:
09:45:43 **** Incremental Build of configuration Debug for project testing ****
Info: Internal Builder is used for build
g++ "-IC:\\opencv_contrib-3.0.0\\modules\\xfeatures2d\\include" "-IC:\\Users\\Amit\\Desktop\\opencv\\build\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\testing.o" "..\\src\\testing.cpp"
g++ "-LC:\\Users\\Amit\\Desktop\\opencv\\build\\x86\\mingw\\lib" -o testing.exe "src\\testing.o" -llibopencv_core310 -llibopencv_imgcodecs310 -llibopencv_imgproc310 -llibopencv_xfeatures2d310 -llibopencv_features2d310 -llibopencv_highgui310
c:/mingw/bin/../lib/gcc/mingw32/4.9.3/../../../libmingw32.a(main.o):(.text.startup+0xa7): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
09:45:43 Build Finished (took 396ms)
Can anyone save me?
Thanks,
Amit.
When you create an executable the linker expects a function named main in the global namespace. You have placed the function inside a namespace instead of the global namespace so the linker will not find it.
So either move your main outside of the sp namespace or tell the linker where the function is (at least that is possible with MS linker but not sure how it is done with g++).
namespace sp {
int main() {
return 0;
}
}
declares an sp::main function, not main. This leaves you without a main function to serve as the program entry point.
Solution: Remove main from the sp namespace.
int main() {
return 0;
}

Eclipse shows error, but project compiles and executes

I'm working in a Eclipse MARS C++ project. The following code compiles and executes perfectly even though Eclipse keeps showing some errors.
The code:
#include <iostream>
#include <ratio>
#include <chrono>
using namespace std;
int main()
{
typedef std::chrono::duration<int> seconds_type;
typedef std::chrono::duration<int,std::milli> milliseconds_type;
typedef std::chrono::duration<int,std::ratio<60*60>> hours_type;
hours_type h_oneday (24);
seconds_type s_oneday (60*60*24);
milliseconds_type ms_oneday (s_oneday);
seconds_type s_onehour (60*60);
hours_type h_onehour (std::chrono::duration_cast<hours_type>(s_onehour));
milliseconds_type ms_onehour (s_onehour);
std::cout << ms_onehour.count() << "ms in 1h" << std::endl;
}
The errors:
Symbol 'duration' could not be resolved.
Type 'std::milli' could not be resolved.
Symbol 'ratio' could not be resolved.
Symbol 'duration_cast' could not be resolved.
Method 'count' could not be resolved.
Invalid overload of 'std::endl'.
I think all the errors are about something with the includes.
The message when building:
Invoking: Cross G++ Compiler
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/Teste.d" -MT"src/Teste.o" -o "src/Teste.o" "../src/Teste.cpp"
Finished building: ../src/Teste.cpp
Building target: Teste
Invoking: Cross G++ Linker
g++ -o "Teste" ./src/Teste.o
Finished building target: Teste
I've already put -std=c++11 on PROJECT > PROPERTIES > C/C++ BUILD > SETTINGS > MISCELLANEOUS.
I've already put __GXX_EXPERIMENTAL_CXX0X__ on C/C++ GENERAL > PATHS AND SYMBOLS > SYMBOLS too.
I've already cleaned and rebuild it, closed and reopened it (project and eclipse), but nothing happens. This is a 1 file project, how can Eclipse find 8 errors in it and still compile and execute it? I think its just the editor messing with me. Can anyone help me to be free from the errors?
Don't trust Eclipse, or Visual Studio or any other IDE for that matter. The parser they use to give you "red squiggles" in the editor is not the real compiler. It has different bugs, it has different levels of standard compliance, it has very little time to parse your code (since it's interactive).
It will give false results.
The 'in-editor' parsers are nice, helpful & convenient tools. But they are not the compiler proper and they should not be interpreted as more than helpful hints.

Error in eclipse boost library

I am trying to setup boost library on my eclipse. I have
Windows 7
Eclipse Luna
Boost 1.59
Mingw
I have already compile boost library with the b2 command.
I also added the library in Paths and Symbols for my project.
In the minGW linker I have the "boostdir"\stage\lib path for the -L options.
In the -l options I tried a lot of combination of
-boost_filesystem
-boost_system
-boost
-...
Here is the problem:
#include<iostream>
#include <boost/math/distributions/chi_squared.hpp>
int main() {
double pvalue = 2.667;
boost::math::chi_squared_distribution aDist(1);
pvalue = boost::math::cdf(aDist,pvalue);
std::cout << "The p-value is : "<<pvalue << std::endl;
return 0;
}
The compiler gives me :
16:24:25 **** Incremental Build of configuration Debug for project test2 ****
Info: Internal Builder is used for build
g++ "-IC:\\MinGW\\boost_1_59_0" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\test2.o" "..\\src\\test2.cpp"
..\src\test2.cpp: In function 'int main()':
..\src\test2.cpp:6:40: error: missing template arguments before 'aDist'
boost::math::chi_squared_distribution aDist(1);
^
..\src\test2.cpp:6:40: error: expected ';' before 'aDist'
..\src\test2.cpp:7:28: error: 'aDist' was not declared in this scope
pvalue = boost::math::cdf(aDist,pvalue);
^
16:24:28 Build Finished (took 3s.531ms)
The second error is ok since it is cause by line 6.
But the auto completion of eclipse works so he can somehow see the library!
Where is the error coming from?
boost::math::chi_squared_distribution is a class template. You need to provide a template parameter for it. For exampe,
boost::math::chi_squared_distribution<some_floating_point_type> mydist(1);

Add C++ Library to eclipse C++ Project

I am attempting to add an existing library to Eclipse. I use a cross compiler for C++ with the Eclipse IDE, installed on a virtual linux debian machine.
The mmapGpio lib is found here.
/mmapGpioBasicRev1.tar.gz has a cpp and an h file with a small demo program.
I have compiled this code without a problem. A .o file is generated. I've archived the file successfully with ar -q libmmapGpio.a mmapgpio.o
I've placed my libmmapGpio.a in ~/.../UserLib directory
I've placed my mmapGpio.h in ~/.../UserInclude
At this point all is OK.
I open a new project that uses the mmapGpio library:
#include "mmapGpio.h"
#include "stdio.h"
int main(void){
mmapGpio rpiGpio; // instantiate an instance of the mmapGpio class
rpiGpio.setPinDir(17,mmapGpio::OUTPUT); // set GPIO17 to output
while(1) {// toggle pin as fast as possible
rpiGpio.writePinHigh(17);
rpiGpio.writePinLow(17);
}
return 0;
}
So cross-compilation is done, but linker say cannot find -llibmapGpio!
I have made declaration in the properties project; C/C++ General
includes path : /home/octopuss/rpi/UserInclude (the mmapGpio.h file)
Library path : /home/octopuss/rpi/UserLib (the libmmapGpio.a file)
Libraries : libmmapGpio
Why do I receive this message?
for detail -> console view
03:16:30 **** Build of configuration Debug for project Gpio1 ****
make all
Building file: ../Gpio1.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/home/octopuss/rpi/UserInclude -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Gpio1.d" -MT"Gpio1.d" -o "Gpio1.o" "../Gpio1.cpp"
Finished building: ../Gpio1.cpp
Building target: Gpio1
Invoking: Cross G++ Linker
arm-linux-gnueabihf-g++ -L/home/octopuss/rpi/UserLib -o "Gpio1" ./Gpio1.o -lmmapGpio
/home/octopuss/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../lib/gcc/arm-linux-gnueabihf/4.8.3/../../../../arm-linux-gnueabihf/bin/ld: skipping incompatible /home/octopuss/rpi/UserLib/libmmapGpio.so when searching for -lmmapGpio
/home/octopuss/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../lib/gcc/arm-linux-gnueabihf/4.8.3/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lmmapGpio
collect2: error: ld returned 1 exit status
make: *** [Gpio1] Erreur 1
error :
skipping incompatible /home/.../UserLib/libmmapGpio.so when searching for -lmmapGpio
ld: cannot find -lmmapGpio
"why this message ?"
It's because with your settings, the linker actually looks up for a library file named liblibmmapGpio.a.
"... so crosscompilation is done but linker say "cannot find -llibmapGpio" !
...
- Libraries : libmmapGpio"
You just need to specify the library without the lib prefix in the linker library settings:
mmapGpio
The Eclipse CDT Builder passes this as a -l option to the linker, which automatically extends to search for libmmapGpio.a at the specified additional pathes.
See also this Q&A for more illustrated samples and links:
Problems importing libraries to my c++ project, how to fix this?
I found the problem ... my .so lib wasn't ARM cross compiled so there is a X86 library not compatible whith my ARM Programm.
I solve this to set erm-linuxgnuabihf- prefix and his path to cross setting parameter.
Thanks to TTAVAR PEI and Scott Stensland
enjoy

Eclipse c++ build error: no such file or directory

I installed MinGW and CDT following some tutorials. I'm trying to compile and run "hello world" code. There are no errors in eclipse but when I compile the code I get this error:
22:48:32 **** Incremental Build of configuration Debug for project test3 ****
Info: Internal Builder is used for build
g++ "-IC:\\MinGW\\lib\\gcc\\mingw32\\4.8.1\\include\\c++" "-IC:\\MinGW\\lib\\gcc\\mingw32\\4.8.1\\include\\c++\\mingw32" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\test3.o" "..\\src\\test3.cpp"
g++: error: CreateProcess: No such file or directory
22:48:32 Build Finished (took 135ms)
The code:
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
Might be a silly question to ask, but did you save the file somewhere before building it? I've had an error like this that was solved by saving it on my Desktop or wherever.
Edit: There seems to be several people with problems using MinGW. You mentioned you looked at previous stackoverflow questions, do they include these:
Eclipse CDT error: Unable to compile
MinGW error: No such file or directory exists
Other solutions suggest you should try compiling the program via command line, which...shouldn't be a problem since it's just "Hello, World".