Eclipse shows error, but project compiles and executes - c++

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.

Related

undefined reference to `WinMain' in eclipse IDE with cygwin (setup-x86_64)

I am currently doing unit testing for a project of c/c++ in eclipse IDE (Version: Luna Service Release 2 (4.4.2)) and I have recently upgraded my cygwin to cygwin64 to make it compatible with c++17 standard and my g++ version is g++ (GCC) 10.2.0, But I am not able to build my testsuites in eclipse, I have set the environment path to "C:\cygwin64\bin", But still I am getting the below error. Could you please help me with the below error:
Console output after building my project :
make all
Building file:
C:/Accurev/AngioJet_Development_ver1/CS/AngioJet/Firmware/src/ClinicalFeatures/door.cpp
Invoking: Cygwin C++ Compiler
g++ -DUNIT_TESTING - "C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/build/PicoZed_FSBL_bsp/ps7_cortexa9_0/include" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../testsupport/" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../testsupport/Source" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/src/Platform/Zynq7000" - I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/src/HostInterface" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/soup/treck/6_0_1_56/include" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../Einstein/src/Platform/Zynq7000" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../Einstein/src/Common" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/src/Common" -I"C:/Accurev/AngioJet_Development_ver1/CS/ut/testsuites/Firmware/hell/../../../../AngioJet/Firmware/src/ClinicalFeatures" -O0 -g -fprofile-arcs -ftest-coverage -Wall -c -fmessage-length=0 -MMD -MP -MF"src/door.d" -MT"src/door.d" -o "src/door.o" "C:/Accurev/AngioJet_Development_ver1/CS/AngioJet/Firmware/src/ClinicalFeatures/door.cpp"
Finished building: C:/Accurev/AngioJet_Development_ver1/CS/AngioJet/Firmware/src/ClinicalFeatures/door.cpp
Building target: helll.exe
Invoking: Cygwin C++ Linker
g++ -fprofile-arcs -o "helll.exe" ./src/door.o -lgcov -lbfd
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-cygwin/10/../../../../lib/libcygwin.a(libcmain.o): in function `main':
/usr/src/debug/cygwin-3.3.6-1/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
collect2: error: ld returned 1 exit status
make: *** [makefile:47: helll.exe] Error 1
07:05:17 Build Finished (took 4s.531ms)
`*
I have uninstalled and reinstalled the softwares. Also I have built a new project from scratch which is building fine but whenever i try to build my project it is not building.
While doing unit testing in eclipse we create the project as c++ project and import the source files mentioned below and the structure is as follow:
Source file - door.cpp, door.h
door.h:
#include "global.h"
namespace lDOOR { void close( BasicTypes::UInt8 mode ); }
door.cpp:
#include "door.h"
void lDOOR::close(void)
{
//Normal Mode
close(BscMsg::NORMAL);
}
and we do the unit testing for the above functions present in .cpp file in the file created by us door_testsuite.h which is as follows:
#include <cxxtest/TestSuite.h>
class door_TestSuite: public CxxTest::TestSuite {
public:
void setUp(){}
void teardown(){}
void testDoorLDoorClose()
{
BscMsg::UInt8 mode = 2;
lDOOR::close(mode);
TS_ASSERT_EQUALS();
}};
And when I test the above code in cygwin terminal it executes completely fine.
This means there is no WinMain function, which is the entry point to Windows windows programs. Yes, confusing. You can choose to either build a program as a console program or windows program. (There are a few other types that are irrelevant) In a console program, the entry point is main (As with the entire world of computing), but in a windows program, it is WinMain. This question has a good answer that goes into depth about it, but you either need to switch the type of program to console, or (If your program is creating and using windows) add a WinMain function.
Sorry if this is confusing, it is difficult to differentiate between Windows, the OS, and windows, the type of program. Also, my knowledge on this subject is all from VSCode, so may be completely or partially incorrect.
TLDR: Either add a WinMain function, or switch to building your program as a windows program.

Unexpected result when initializing variable in C++ using curly braces

I'm using atom to practice C++ (I'm very new). I just learned to initialize variables like the following:
#include <iostream>
using namespace std;
int main() {
int myInt {};
return 0;
}
When I build and run the previous code in codelite I receive no errors. However, if I compile my atom file dailyPractice10.cpp using my MacBook terminal (zsh) I get the following error:
dailyPractice10.cpp:7:12: error: expected ';' at end of declaration
int myInt {};
^
;
1 error generated.
I'm using the following command to compile it on terminal:
g++ -o dailyPractice10 dailyPractice10.cpp (compiles)
./dailyPractice10 (runs program)
Does anyone have any feedback why this code runs in codelite but doesn't compile in terminal?
Because this feature is added from c++11.
if you will like to try below command.it will work.
$ g++ -std=c++0x -o dailyPractice10 dailyPractice10.cpp
The key to fixing this issue is to set the C++11 (or above) standards while building your code.
In the console tab of the IDE, the following output is generated before the error. Notice that no standard is being defined while building the code:
make all
Building file: ../1.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"1.d" -MT"1.o" -o "1.o" "../1.cpp"
We need to add the --std=c++1x flag to the g++ command. The following solution is for the ones using the Eclipse IDE and the MacOSX C++ compiler:
Right click on the project from the "Project Explorer".
Go to Properties > C/C++ Build > Settings.
Under the "Tool Settings" tab, find "GCC C++ Compiler" > "Miscellaneous"
In the "Other Flags" text box, edit the text such that it looks like:
-std=c++17 -c -fmessage-length=0
If you intend to use any other c++ standard, replace "c++17" with the standard of your choice ( eg. c++20).
Apply Changes.
Run Clean, and the Build again.
you should try this to compile the Code
g++ -std=c++20 -o dailyPractice10 dailyPractice10.cpp

Eclipse C++ on OSX Cant Build

I am new to coding and C++ and have just installed C++ and XCode on my computer. I have made Mac OSX GCC my preferred tool chain.
I can't seem to even get a simple Hello World program to run, I really need some help here, I've posted before and looked around but nothing seems to help me.
Code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world \n";
return 0;
}
Error after clicking on build all:
12:38:22 **** Incremental Build of configuration Debug for project Test1 ****
make all
Building file: ../test.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"test.d" -MT"test.o" -o "test.o" "../test.cpp"
Finished building: ../test.cpp
Building target: Test1
Invoking: MacOS X C++ Linker
g++ -o "Test1" ./test.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Test1] Error 1
12:38:37 Build Finished (took 15s.286ms)
You mentioned that you just installed XCode. If you are looking to get started with C++ programming, using XCode will make your experience more satisfying and learning curve faster than if you use Eclipse. XCode is a "native" IDE on Mac.
Having said this, I understand there may be reasons to use Eclipse, e.g. if you expect to start programming on non-Apple platforms in a very near future, or want to familiarize yourself with Eclipse as the IDE to do Android programming. However, if you are going to be on Mac for awhile and want to pick up C++ faster, I would strongly recommend sticking with XCode.
Now, the error you show could be because the file has not been saved, as suggested by one of the commenters. That's exactly the error you get if main() is missing.

Eclipse C++ add shared library to main project

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).

GoogleTest 1.6 with Cygwin 1.7 compile error: 'fileno' was not declared in this scope

GoogleTest 1.6 with Cygwin 1.7: 'fileno' was not declared in this scope
Error message when building a simple test on Factorial() function in Eclipse CDT:
Invoking: Cygwin C++ Compiler
g++ -std=c++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp"
In file included from E:\source\gtest-1.6.0\include/gtest/internal/gtest-internal.h:40:0,
from E:\source\gtest-1.6.0\include/gtest/gtest.h:57,
from ../src/challenge.cpp:11:
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::FileNo(FILE*)':
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1589:51: error: 'fileno' was not declared in this scope
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1595:57: error: 'strdup' was not declared in this scope
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1627:71: error: 'fdopen' was not declared in this scope
Eclipse CDT 8.1 running gcc 4.7.3 on Cygwin 1.7.22
gTest 1.6 succesfully built including demo tests, with cmake 2.8.9 on Cygwin 1.7.22
I've linked the built lib with full path, E:\lib\gtest-1.6.0\Cygwin\libgtest.a
The following command option was added manually, got same error without it.
-DGTEST_OS_CYGWIN=1
Seems the errors have nothing to do with my code. Anyone using gTest with Eclipse and Cygwin?
Thank you,
unsigned long Factorial(unsigned n) {
return n==0? 0 : n*Factorial(n-1);
}
// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(1, Factorial(0));
}
// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}
Setting the C++ standard to -std=gnu++0x rather than -std=c++0x, worked for me. You can try the statement:
g++ -std=gnu++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp"
Setting symbol (-DGTEST_OS_CYGWIN=1) has got nothing to do with this error.
Some functions go beyond the ANSI standard.
These are disabled when you use std=c++11 (or std=c++0x).
Among them are fdopen, fileno and strdup.
There are two possibilities to use them:
Use the GNU dialect (std=gnu++11).
If you want to compile without dialect and make a local exception, you can include stdio.h with the __STRICT_ANSI__ undefined. (see: Error "'fdopen' was not declared" found with g++ 4 that compiled with g++3)
I have tested both on Suse Linux Enterprise 11, MinGW and Cygwin.
Addition: Another (possibly better) way to access non-ANSI symbols would be to add
#define _POSIX_C_SOURCE 200809L
before the first #include in your file. This will give you access to most of the non-standard routines.
Some functions (e.g. realpath(...)) require
#define _BSD_SOURCE
to be inserted on top of your file.