Compiling cpp code in netbeans produce errors, how to solve it? - c++

i use the netbeans with MinGW and MYSY make /debugger but when i compile a basic cpp code in it and run it it produces two erorrs
this is the code runned and the output![alt text][1] box
#include <iostream>
void main()
{
cout << "Hello World!" << endl;
cout << "Welcome to C++ Programming" << endl;
}
output is
/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/d/Users/Home/Documents/NetBeansProjects/newApp'
/usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/newapp.exe
make[2]: Entering directory `/d/Users/Home/Documents/NetBeansProjects/newApp'
mkdir -p dist/Debug/MinGW-Windows
g++.exe -o dist/Debug/MinGW-Windows/newapp build/Debug/MinGW-Windows/newmain.o build/Debug/MinGW-Windows/newfile.o build/Debug/MinGW-Windows/main.o
build/Debug/MinGW-Windows/newfile.o: In function `main':
D:/Users/Home/Documents/NetBeansProjects/newApp/newfile.cpp:5: multiple definition of `main'
build/Debug/MinGW-Windows/newmain.o:D:/Users/Home/Documents/NetBeansProjects/newApp/newmain.c:15: first defined here
build/Debug/MinGW-Windows/main.o: In function `main':
D:/Users/Home/Documents/NetBeansProjects/newApp/main.cpp:13: multiple definition of `main'
build/Debug/MinGW-Windows/newmain.o:D:/Users/Home/Documents/NetBeansProjects/newApp/newmain.c:15: first defined here
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/MinGW-Windows/newapp.exe] Error 1
make[2]: Leaving directory `/d/Users/Home/Documents/NetBeansProjects/newApp'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/d/Users/Home/Documents/NetBeansProjects/newApp'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
how can i solve this ?

I can see three things here, the first two have been pointed out by Xavier and jwismar, but to consolidate:
Both D:/Users/Home/Documents/NetBeansProjects/newApp/newfile.cpp and D:/Users/Home/Documents/NetBeansProjects/newApp/main.cpp define a main(). You'll need to remove one of these from your project.
You'll need to be using std::cout and using std::endl.
main() should always return an int.

You have two different files in your project that define main(). One is called newfile.cpp, the other one is called newmain.c
And as the earlier comment notes, you will need to specify std::cout and std::endl

Related

C++ boost error

I'm building a code based on this fast factorial library, and it depends on boost and mpir. I'm running Ubuntu 14.04 and using Netbeans 8.0.2. To simply test the #include statements of the library I made this silly code:
#include <iostream>
#include <mpir.h>
#include <primeswing.h>
#include <xmath.h>
/*
*
*/
int main() {
long x = 4;
std::cout << x;
return 0;
}
When I try to compile it however I get the following error
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/arengorn/NetBeansProjects/BA'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/ba
make[2]: Entering directory `/home/arengorn/NetBeansProjects/BA'
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/ba build/Debug/GNU-Linux-x86/main.o ->lm
build/Debug/GNU-Linux-x86/main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/ba] Error 1
make[2]: Leaving directory `/home/arengorn/NetBeansProjects/BA'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/arengorn/NetBeansProjects/BA'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 114ms)
Can anyone help me with why is the boost library (which is installed and present under /usr/include) is not working?
As commented by Wintermute
You need to link libboost_system.so. Put -lboost_system in your linker flags.
That solved the problem.

Netbeans Building Issues for C++

I'm using the MinGW complier/make/etc files but whenever I try to run my code it spits this back out to me:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/c/Users/Geneva/workspace/C++ Through Game Programming'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/c___through_game_programming.exe
make[2]: Entering directory `/c/Users/Geneva/workspace/C++ Through Game Programming'
mkdir -p dist/Debug/MinGW-Windows
g++ -o dist/Debug/MinGW-Windows/c___through_game_programming build/Debug/MinGW-Windows/diceroll.o build/Debug/MinGW-Windows/main.o
build/Debug/MinGW-Windows/main.o: In function `main':
C:\Users\Geneva\workspace\C++ Through Game Programming/main.cpp:35: multiple definition of `main'
build/Debug/MinGW-Windows/diceroll.o:C:\Users\Geneva\workspace\C++ Through Game Programming/diceroll.cpp:16: first defined here
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/MinGW-Windows/c___through_game_programming.exe] Error 1
make[2]: Leaving directory `/c/Users/Geneva/workspace/C++ Through Game Programming'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/c/Users/Geneva/workspace/C++ Through Game Programming'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
I have my environment variable PATH set right, when I installed the gbd I just extracted it to the normal C:\MinGW\bin so that I wouldn't have to do C:\MinGW\msys\1.0\bin
Can anyone help out? If I can't figure this out I'm going to go back to coding in Java
The compiler message is clear: You have defined multiple main() functions

getting an error of BUILD_FAIL

I am using netbeans IDE on ubuntu for c++ and whenever i am running a program i am getting an error of BUILD_FAIL
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/rupesh/NetBeansProjects/Cpp1'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/cpp1
make[2]: Entering directory `/home/rupesh/NetBeansProjects/Cpp1'
mkdir -p dist/Debug/GNU-Linux-x86
g++-o dist/Debug/GNU-Linux-x86/cpp1 build/Debug/GNU-Linux-x86/main.o build/Debug/GNU- Linux-x86/1.o
build/Debug/GNU-Linux-x86/1.o: In function `main':
/home/rupesh/NetBeansProjects/Cpp1/1.cpp:4: multiple definition of `main'
build/Debug/GNU-Linux-x86/main.o:/home/rupesh/NetBeansProjects/Cpp1/main.cpp:15: first defined here
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/cpp1] Error 1
make[2]: Leaving directory `/home/rupesh/NetBeansProjects/Cpp1'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/rupesh/NetBeansProjects/Cpp1'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 190ms)
I am running a simple program to print hello world.
From what the compiler is spitting out, it seems you have two files that specify a main() being built as one project.
/home/rupesh/NetBeansProjects/Cpp1/1.cpp:4: multiple definition of
`main'
build/Debug/GNU-Linux-x86/main.o:/home/rupesh/NetBeansProjects/Cpp1/main.cpp:15:
first defined here collect2: ld returned 1 exit status make[2]: *
[dist/Debug/GNU-Linux-x86/cpp1] Error 1
check your folders to make sure you have no other files that the compiler is attempting to build in with main.cpp, in this case look for a 1.cpp

Error in C++ netbeans mkdir not found?

Everytime I try to build my c++ file I get this error
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/c/Users/Joey/Documents/NetBeansProjects/C++Train'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/c__train.exe
make[2]: Entering directory `/c/Users/Joey/Documents/NetBeansProjects/C++Train'
make[2]: mkdir: Command not found
mkdir -p build/Debug/MinGW-Windows
make[2]: Leaving directory `/c/Users/Joey/Documents/NetBeansProjects/C++Train'
make[1]: Leaving directory `/c/Users/Joey/Documents/NetBeansProjects/C++Train'
make[2]: *** [build/Debug/MinGW-Windows/HelloWorld.o] Error 127
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 2s)
I checked the directory the make.exe was in and mkdir was there so I'm just baffled.
my code itself shows no errors:
#include <iostream>
int main() {
using namespace std;
cout << "Hello World!" << endl;
return 0;
}
Just a simple little hello world to test netbeans.
I guess you didn't add msys to your PATH variable. See this entry in the Netbeans forum(http://forums.netbeans.org/topic38061.html) So, the error message you received does not complain about make, it complains that it can't find mkdir, which is supposed to be in a directory in your msys directory. Adding C:\MinGW\msys\1.0\bin\ to your windows PATH variable will probably be sufficient to fix this.

undefined reference to `u_fopen_48'

I'm new to c/c++ and I guess I have some basic problem. I get undefined reference to u_fopen_48' error when compiling:
#include <unicode/ustdio.h>
int main(int argc, char** argv) {
UFILE* ufile = u_fopen("/home/emstol/Desktop/utf8demo.txt", "r", NULL, "utf8");
return 0;
}
Doc for this function is here. I'm using ICU 4.8.1 (compiled myself, step by step according to readme.html ;)), NetBeans with g++ underneath. If it helped this is what I see during building:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/emstol/NetBeansProjects/TextFairy1'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/textfairy1
make[2]: Entering directory `/home/emstol/NetBeansProjects/TextFairy1'
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/textfairy1 build/Debug/GNU-Linux-x86/main.o
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/emstol/NetBeansProjects/TextFairy1/main.cpp:4: undefined reference to `u_fopen_48'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/textfairy1] Error 1
make[2]: Leaving directory `/home/emstol/NetBeansProjects/TextFairy1'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/emstol/NetBeansProjects/TextFairy1'
make: *** [.build-impl] Error 2
You seem to have forgotten to link the library you used. You should refer to This page for instructions.
When building composite projects the compiler can't just easily find all it's required references. Most libraries come in the form of a shared object file (.so) and without their C code to be compiled along with the rest of your project, while only supplying the headers for their functions. This allows the compiler to create "sockets" in the code for the functions to be placed into, but without telling the linker where these functions should be taken from - the link process will simply fail.
You must, therefore, explicitly tell the linker where to search for the symbols it will seek, and this is usually done with the -l flag, though it would appear the ICU library has taken a somewhat different approach to it.