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.
Related
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;
}
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.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is my code and I get the following error.
Code
#include <vector>
#include <iostream>
//...
using namespace std;
main(){
vector<int> arrayi;
int i = 999; // some integer value
arrayi.reserve(10); // make room for 10 elements
arrayi.push_back(i);
cout<<arrayi.capacity()<<endl;
cout<<arrayi.size()<<endl;
}
Error
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
error: 'arrayi' does not name a type|
error: 'arrayi' does not name a type|
error: 'cout' does not name a type|
error: 'cout' does not name a type|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
where did I go wrong??
You may not have expression statements in the namespace-/file-scope. Only declaration statements are allowed.
Declare a function, and write the expressions in the block scope of that function. In particular, I suggest declaring the main function, because a C++ program must contain one. Main function is the entry point of the program.
Your code is in global namespace, it should go in a function like main().
#include <vector>
#include <iostream>
int main()
{
std::vector<int> arrayi;
int i = 999; // some integer value
arrayi.reserve(10); // make room for 10 elements
arrayi.push_back(i);
std::cout << arrayi.capacity() << std::endl;
std::cout << arrayi.size() << std::endl;
}
Demo
This question already has answers here:
create many cpp files in one project [closed]
(4 answers)
Closed 7 years ago.
I have 2 files and i want to compile and run them using make command. I created a Makefile named "Makefile". They are compiled but shows an error
all: hello
hello: pgm1.o pgm2.o
g++ pgm1.o pgm2.o -o hello
pgm1.o: pgm1.cpp
g++ -c pgm1.cpp
pgm2.o: pgm2.cpp
g++ -c pgm2.cpp
They are compiled but shows an error
make -f Makefile
g++ pgm1.o pgm2.o -o hello
pgm2.o: In function `print2()':
pgm2.cpp:(.text+0x0): multiple definition of `print2()'
pgm1.o:pgm1.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [hello] Error 1
pgm1.cpp
#include <iostream>
#include "pgm2.cpp"
using namespace std;
int main()
{
cout<<"Thiss is program 1";
print2();
return 0;
}
<>pgm2.cpp
#include <iostream>
using namespace std;
void print2()
{
cout<<"Thiss is program 2";
}
What is that error? How can i rectify it?
You are compiling both these files into singular output file but your pgm1.cpp already contains the function print2() by virtue of the line #include "pgm2.cpp"...
Possible solutions can be:
1) Remove the include file and instead add a function declaration.
void print2();
2) As already pointed out create a header file and use include it instead of a .cpp file.
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.