While doing programming in Code::Blocks it does not compile for C++. Even for a this kind of a simple program.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
it gives those errors.
=== Build: Debug in start (compiler: GNU GCC Compiler) ===
obj\Debug\start.o||In function `main':
C:\Users\dp\Desktop\c++\start\start.cpp|4|multiple definition of `main'
obj\Debug\main.o:C:\Users\dp\Desktop\c++\start\main.cpp|6|first defined here
error: ld returned 1 exit status
=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 6 second(s)) ===
what can i do to fix this.
As-is the code itself should compile fine. But as the error suggests you have multiple int main() function definitions across multiple source files. One is in the start.cpp and the other is in the main.cpp file. Either keep only one main() entry point or compile a single file. Compile with g++ front-end, not with gcc.
Related
I have a mini project with two files:
main.cpp
#include <string>
template<int I>
int getint(int i)
{
extern std::string var;
return var.size() * i;
}
int main()
{
return getint<2>(2);
}
and sub.cpp
#include <string>
extern std::string var;
std::string var = "Hello";
But when I compile the project, the linker gives the error:
-------------- Clean: Debug in test (compiler: GNU GCC Compiler)---------------
Cleaned "test - Debug"
-------------- Build: Debug in test (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -fexceptions -g -c C:\Users\Fan\Downloads\test\test\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -Wall -fexceptions -g -c C:\Users\Fan\Downloads\test\test\sub.cpp -o obj\Debug\sub.o
mingw32-g++.exe -o bin\Debug\test.exe obj\Debug\main.o obj\Debug\sub.o
obj\Debug\main.o: In function `Z6getintILi2EEii':
C:/Users/Fan/Downloads/test/test/main.cpp:6: undefined reference to `var'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
I have no clue how this happens, because as long as I simplified the program a little, the problem goes away.
If I make getint() a plain old function instead of a function template, then the compilation succeeds.
If I move the line "extern std::string var;" outside the function (say to one line above it), then the compilation succeeds.
If I change the type of var to an int (and omit ".size()" in main.cpp, and assign 1 to it in sub.cpp), then the compilation succeeds.
It seems a weird combination of factors that prevent my original project from compiling. I have no idea what they are :(
Edit: In response to the "cannot reproduce" remark, I am trying the compilation on my local windows 7 machine using Code::Blocks 17.12. Output of the compiler and the linker has been updated to include compiler flags.
I'm getting an error while attempting to compile a C++ project on Code::Blocks 17.12 (Windows 10, default MinGW GCC compiler). The function
std::vector<int> crib_drag(const std::string&, const std::vector<int>&);
is declared in crib_drag.h, which I created through the C::B dialog (crib_drag.h #includes vector and string). The function is defined in crib_drag.cpp, also created through C::B dialog:
#include "crib_drag.h"
std::vector<int> crib_drag(const std::string& term, const std::vector<int>& xored)
{
std::vector<int> ret;
//Do some stuff
return ret;
}
main.cpp includes crib_drag.h with
#include "crib_drag.h"
In main, crib_drag is called.
Compiling gives an undefined reference error to the crib_drag function. My understanding is that I do not need to alter the linker settings, as all files were created using default settings within the C::B project, and I have not changed the file paths of any files.
Can anyone explain what is going wrong here and how to fix it? Thanks in advance.
The build log reads as follows:
-------------- Build: Debug in CribDrag (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -o bin\Debug\CribDrag.exe obj\Debug\crib_drag.o obj\Debug\main.o
obj\Debug\main.o: In function `main':
C:/[Long file path omitted]/CribDrag/main.cpp:13: undefined reference to `crib_drag(std::string const&, std::vector<int, std::allocator<int> > const&)'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
main:
#include <iostream>
#include "crib_drag.h"
#include <vector>
using namespace std;
int main() {
vector<int> xored1{14, 6, 31, 30, [omitted for brevity]};
string word("overnight");
vector<int> found_at = crib_drag(word, xored1);//Problem line
//.....
return 0;
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
So I've got a project file in CodeBlocks for Project Euler, but I'm a bit confused about what I've done wrong in setting my code up. I have a main.cpp file for running my programs, and I prototype each problem's function before I use it in the main block. However, I have this error when trying to build it:
||=== Build: Debug in Project Euler (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function main':|
C:\Users\under\cpp-workspace\Project Euler\main.cpp|9|undefined reference top4()'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
This is what I have, so I'm not sure what's wrong?
main.cpp:
#include <iostream>
using namespace std;
void p4();
int main()
{
p4();
return 0;
}
p4.cpp:
#include <iostream>
using namespace std;
void p4()
{
cout << "hello there" << endl;
}
I'm not sure what's wrong?
My question is not a duplicate, at least not that I can tell. The question this is supposedly a duplicate of never mentions the issue I'm having.
I've checked, and p4() is a void function with no inputs, so I'm not sure what's wrong.
You have declared p4() but you haven't defined it. Add
void p4()
{
}
to your file for an empty definition. If you need to do more in p4, add whatever code you want to.
Update
You just need to add p4.cpp to the set of files in your project.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm just learning constructor and destructor I'm following the tutorial this guy is doing bucky tutorial. I think the video is outdated? since I followed every single step he says and i'm still getting an error.
main.cpp
#include <iostream>
#include "TESTING.h"
using namespace std;
int main(){
TESTING so;
cout << "TEST" << endl;
}
TESTING.h
#ifndef TESTING_H
#define TESTING_H
class TESTING
{
public:
TESTING();
protected:
private:
};
#endif // TESTING_H
TESTING.cpp
#include "TESTING.h"
#include <iostream>
using namespace std;
TESTING::TESTING()
{
cout << "TESTTTTT!!" << endl;
}
Error messages
\main.o:main.cpp undefined reference to 'TESTING::TESTING()'
Build log
mingw32-g++.exe -c D:\C++\TESTING!\main.cpp -o D:\C++\TESTING!\main.o
mingw32-g++.exe -o D:\C++\TESTING!\main.exe D:\C++\TESTING!\main.o
D:\C++\TESTING!\main.o:main.cpp:(.text+0x52): undefined reference to `TESTING::TESTING()'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
1 error(s), 0 warning(s) (0 minute(s), 1 second(s))
You only build and link the main source file, not the TESTING source file. You need to compile TESTING.cpp as well, and then link with TESTING.o:
mingw32-g++.exe -c D:\C++\TESTING!\TESTING.cpp -o D:\C++\TESTING!\TESTING.o
mingw32-g++.exe -o D:\C++\TESTING!\main.exe D:\C++\TESTING!\main.o D:\C++\TESTING!\TESTING.o
You need to include both the compilation units in the build.
That means both source files need to be compiled, and both corresponding object files specified in the link command.
At present, only main.cpp is being compiled to an object, and only main.o is being linked.
When I run and build a simple program, it fails.
Here is the error message:
g++ -Wall -o "main" "main.cpp" (in directory: /home/markuz/Desktop)
/tmp/ccHV9wPu.o: In function main':
main.cpp:(.text+0x11): undefined reference toTest::display()'
collect2: ld returned 1 exit status
Compilation failed.
Here are the files. The compile and build command is the default of geany 1.22
//main.cpp
#include "imba.h"
int main(){
Test t;
t.display();
return 0;
}
//imba.h
class Test{
public:
void display();
};
//imba.cpp
#include <iostream>
#include "imba.h"
void Test::display(){
std::cout << "oi";
}
Any ideas about this?
Thanks.
You need to also add the imba.cpp file in the compilation step. Although you have included the header in your main file, you have not compiled the source for it and so the linker cannot find the object file for imba.cpp - that is what the error is complaining about