CodeBlocks not building my project - c++

Every time I click the 'run' or 'build and run' options in CodeBlocks for Mac OSX I get this dialogue:
I've checked Project > Properties > Build targets, which is what some forum posts said I should do, and all the files are checked but I keep getting the same popup.
This is my build log:
-------------- Build: Release in printarray (compiler: GNU GCC Compiler)---------------
g++ -o bin/Release/printarray obj/Release/arrays.o obj/Release/main.o -s
ld: warning: option -s is obsolete and being ignored
ld: duplicate symbol _anMyArray in obj/Release/main.o and obj/Release/arrays.o for architecture x86_64
collect2: ld returned 1 exit status
and these are my build messages:
These are the files, though I'm not sure if the content has anything to do with the problem (I made sure both Debug and Release were checked when I created the header and function definitions):
main.cpp
#include <iostream>
#include "arrays.h"
int main()
{
using namespace std;
PrintArray(anMyArray);
return 0;
}
arrays.cpp
#include <iostream>
#include "arrays.h"
void PrintArray(int anArray[])
{
using namespace std;
int nElements = sizeof(anArray) / sizeof(anArray[0]);
for (int nIndex=0; nIndex < nElements; nIndex++)
cout << anArray[nIndex] << endl;
}
arrays.h
#ifndef ARRAYS_H
#define ARRAYS_H
int anMyArray[9] = { 4, 6, 7, 3, 8, 2, 1, 9, 5 };
void PrintArray(int anArray[]);
#endif // ARRAYS_H
Any help?

It's because you define the variable anArray in the header file. When its included in two translation units it's defined twice, giving you the duplicate symbol error.
Just declare it in the header file
extern int anMyArray[9];
and define it in one (and only one) source file.

You declare anMyArray in your header file and then include it in both your cpp files, which means your variable is getting declared twice because of header expansion.
Move it to your main.cpp file.

I solved this question by the way below
Settings->Complier...->Toolchain executables
and click the button "Auto-detect"
click "ok"
the problem solved

Related

Defining function with arrays in different files

I want to define a function in a separate C++ file. The function takes in array as an argument.
These are my files.
selectionsort.cpp
#include "selectionsort.hpp"
int selectionsort(int a[]){
int length{};
length = std::size(a);
for(int i{0}; i < length; ++i){
int smallestIndex{i};
for(int j{i+1}; j < length; ++j){
if(a[j] < a[smallestIndex]){
smallestIndex = j;
};
};
std::swap(a[smallestIndex], a[i]);
};
return 0;
};
selectionsort.hpp
#ifndef selectionsort_hpp
#define selectionsort_hpp
int selectionsort(int []);
#endif /* selectionsort_hpp */
main.cpp
#include "io.hpp"
#include "monsters.hpp"
#include "selectionsort.hpp"
#include <iostream>
#include <iterator>
int main(){
int a[]{ -1, -100, 0, 10, 100, -2, 2, 10000, 45, -10000};
selectionsort(a);
std::cout << a[0] << '\n';
std::cout << a[1] << '\n';
return 0;
};
Xcode shows the following error when I run the program.
Undefined symbols for architecture x86_64:
"selectionsort(int*)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Undefined symbol: selectionsort(int*)
However if I put the function definition of selectionsort.cpp inside the main.cpp file, everything works perfectly. I dont understand what is the problem here.
Your .cpp file contains an error that should not compile. As such, I suspect you are not building it in your current setup at all. That would explain why the object code is not being linked into your application, and the linker is unsatisfied.
If you do separate compilation, i.e.
g++ a.cpp -c
g++ b.cpp -c
Now you have two object files, a.o and b.o. To produce a binary, you must link them together:
g++ a.o b.o -o myprogram
In your code you are trying to pass an array:
int selectionsort(int a[]){
int length{};
length = std::size(a);
...
And you simply can't do that. The array decays to a pointer when passed to a function, and you can't call std::size on a pointer. This won't compile, because it has lost array information about the argument (which was encoded in its array type) and cannot determine its size when it is just a pointer. It has NO IDEA how big your array is when inside the function. The only way you could make this work is to change the code and pass in the size of the array along with your array. This is a common need, so there are span classes out there, and one was added to c++20, which basically bundles a pointer and a size together and you might consider using one of those.
If you fix your build system to build all of your code, then you fix the c++ error above (including changing your header declaration to match), then you link all your object code together, that may fix the issue you're seeing.

Xcode: linker command failed with exit code 1 (use -v to see invocation) [C++]

I am running a c++ programs with multiple files (2)
goofing_around.cpp
add.cpp
goofing_around.cpp:
//
// goofing_around.cpp
// new
//
// Created by Chirag Maheshwari on 14/08/18.
// Copyright © 2018 Chirag Maheshwari. All rights reserved.
//
#include <iostream>
int add(int x,int y);
int doubleNumber(int n)
{
return 2*n ;
}
int main()
{
int x;
std::cout << "Enter the number to be doubled: ";
std::cin >> x;
std::cout << doubleNumber(x)<<std::endl;
std::cout << add(3,2) << std::endl;
return 0;
}
add.cpp:
#include <iostream>
int add(int x,int y){
return x+y;
}
And yet I get an error which goes like this:
duplicate symbol _main in:
/Users/chirag/Library/Developer/Xcode/DerivedData/new-hapneuayvrpdonefrpnervwkxysx/Build/Intermediates.noindex/new.build/Debug/new.build/Objects-normal/x86_64/goofing_around-5915963FFFEE024.o
/Users/chirag/Library/Developer/Xcode/DerivedData/new-hapneuayvrpdonefrpnervwkxysx/Build/Intermediates.noindex/new.build/Debug/new.build/Objects-normal/x86_64/goofing_around-93C433489854664D.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Edit: This was weird.The error was there even before I added the add.cpp file.But then I deleted the projects and tried again.And after rewriting all the code,and adding the add file,I deleted the .h file.But only this time it worked,with the exact same code,and including the same function prototype.I did not have to include the add.cpp files either.
Super weird,but does anyone know why?
The problem is that you are not linking well the add method. You have implemented it in add.cpp but you don't add the link to it in the main code. You should include another "include" in goofing_around.cpp, something like
#include "add.cpp";
It should work.
Another observation: there is no need to print the name of the method "add" in the main code, since these things are done in the header files (if you have any). If not, there no sense to write that since you can just link your main code to the add.cpp.

Separate header file and its logic in c++ [duplicate]

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.
Please make me understand how header files works in C++. I am using osx and g++ compiler. I have
main.cpp
#include<iostream>
#include "myfunc.hpp"
using namespace std;
int main() {
square(10);
return 0;
}
myfunc.hpp
#ifndef MYFUNC_HPP_
#define MYFUNC_HPP_
/*
void square(int x) {
std::cout << x * x << std::endl;
};
*/
void square(int);
#endif // MYFUNC_HPP_
myfunc.cpp
#include<iostream>
#include "myfunc.hpp"
using namespace std;
void square(int x) {
cout << x * x << endl;
}
Now when I am trying to compile using g++ main.cpp , its giving
Undefined symbols for architecture x86_64:
"square(int)", referenced from:
_main in main-088331.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Because it is not able to find the function definition of square that is defined in myfunc.cpp.
But, if I defined square function in header file, it works because now it finds the function definition.
I want to use the function defined in myfunc.cpp in main.cpp, so I am using the header file myfunc.hpp. How can I achieve this? Am I doing something wrong here? Maybe my concept is not that clear about headers since I am new to C++ programming.
When you call g++ main.cpp, the compiler will try to compile and link the program, yet for linking, it lacks the source- or object file containing the definition of square. So it could compile main.cpp based on the function prototype given in the header file, yet it cannot link then.
To just compile main.cpp write
g++ -c main.cpp
To compile and link the complete program write:
g++ main.cpp myfunc.cpp
For more details concerning programs comprising several translation units confer, for example, this link.

How to link multiple files in Turbo C++?

I know Turbo C++ is oudated as hell, but so is the curriculum of our central board in my country (India). And I am doing a school project. And I don't have the freedom to choose my own IDE and compiler. Go figure.
NOTE: I am using Turbo C++ 3.0 in DOSBox in Win10
Anyway, here is the project directory I made to test TC++'s linking:
TC/BIN
-MAIN.CPP
#include <iostream.h>
#include <conio.h>
#include "CL.H"
int main()
{
clrscr();
cout<<"HW";
cl c;
c.set(5);
cout<<c.get();
getch();
return 0;
}
-CL.CPP
#include "CL.H"
void cl::set( int i )
{
a = i;
}
int cl::get()
{
return a;
}
-CL.H
#ifndef CL_H
#define CL_H
class cl
{
int a;
public:
void set( int i);
int get();
};
#endif
All of these compile fine. Upon trying to link, I get the following linker error:
LINKER ERROR: Undefined symbol cl::get() in module MAIN.CPP
LINKER ERROR: Undefined symbol cl::set( int ) in module MAIN.CPP
You can do that:
1- Open TC.exe
2- From project select Open Project
3- Enter the name of project eg: MyProj.prj and Press ok.
4- From project select Add item
5- Locate all the source files and add them.
6- compile and build.
(Posted on behalf of the question author).
I had the .h files also added to the project. That caused all the trouble. Removing the .h files from the project seems to make it work.

Xcode linker command failed issue

Ld /Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer- aeoygjukxhrzaxddvctkqcefsiql/Build/Products/Debug/NBAPlayer normal x86_64
cd /Users/noahheath/Documents/NBAPlayer
export MACOSX_DEPLOYMENT_TARGET=10.9
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -L/Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Products/Debug -F/Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Products/Debug -filelist /Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Intermediates/NBAPlayer.build/Debug/NBAPlayer.build/Objects-normal/x86_64/NBAPlayer.LinkFileList -mmacosx-version-min=10.9 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Intermediates/NBAPlayer.build/Debug/NBAPlayer.build/Objects-normal/x86_64/NBAPlayer_dependency_info.dat -o /Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Products/Debug/NBAPlayer
duplicate symbol __ZN9NFLplayerC1Ev in:
/Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Intermediates/NBAPlayer.build/Debug/NBAPlayer.build/Objects-normal/x86_64/UnsortedStruct.o
/Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Intermediates/NBAPlayer.build/Debug/NBAPlayer.build/Objects-normal/x86_64/main.o
duplicate symbol __ZN9NFLplayerC2Ev in:
/Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Intermediates/NBAPlayer.build/Debug/NBAPlayer.build/Objects-normal/x86_64/UnsortedStruct.o
/Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Intermediates/NBAPlayer.build/Debug/NBAPlayer.build/Objects-normal/x86_64/main.o
duplicate symbol __ZN9NFLplayer8ComparedES_ in:
/Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Intermediates/NBAPlayer.build/Debug/NBAPlayer.build/Objects-normal/x86_64/UnsortedStruct.o
/Users/noahheath/Library/Developer/Xcode/DerivedData/NBAPlayer-aeoygjukxhrzaxddvctkqcefsiql/Build/Intermediates/NBAPlayer.build/Debug/NBAPlayer.build/Objects-normal/x86_64/main.o
ld: 3 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This is the script I have received from xcode and I cannot find the error to save my life. Can someone help me debug this? Forgive me if this isn't presented in the correct format. This is my first time using this website.
Here is my main code and here is the unsorted struct class
#include "NFL.h"
class UnsortedStruct
{
public:
UnsortedStruct();
int GetLength() const;
bool IsFull() const;
void EmptyList();
void InsertItem(NFLplayer nflplayers);
void DeleteItem(NFLplayer nflplayers);
void ResetList();
NFLplayer GetItem(NFLplayer nflPlayers, bool& found);
NFLplayer GetNextItem();
private:
int length;
int currentPos;
NFLplayer NFLlist[MAX_PLAYERS];
};
#include <string>
using namespace std;
const int MAX_PLAYERS = 10;
enum RelationType1 {LESS1, GREATER1, EQUAL1};
struct NFLplayer//describes the set of information for a NFL player.
{
string position, school, name, team;
RelationType1 Compared(NFLplayer);
NFLplayer();
};
NFLplayer::NFLplayer()
{
position=" ";
school=" ";
name=" ";
team=" ";
}
RelationType1 NFLplayer::Compared(NFLplayer players)
{
if(name < players.name)
return LESS1;
else if(name > players.name)
return GREATER1;
else
return EQUAL1;
}
You declared your constructor and your Compared function inside the .h file. Move them into a .cpp file and things should run smoothly.
A more insight on what's happening :
.h files are copy pasted into every object that is being compiled . So the main.o and UnsortedStruct.o will have the header file copy-pasted inside their object at preprocessor time.
You have two objects, and in each object you define the ::Compared and ::NFLPlayer functions implementation. You end up with two implementations of the same function, hence the duplicate symbol.
The .h files are meant to provide definitions for to-be-used structures, classes, functions (it's a forward declaration so to speak).
Implementation bodies are implemented in .cpp files , which in turn will be compiled then linked throughout the code after compilation.
When you see implementations in .h files, you will see that these are inside the class definition :
Class A
{
void foo() { // do something };
}
This is valid, because that function will become inlined.
However, declaring void foo ,then implementing it as A::foo() in the same file will be troublesome if the file is included as header in multiple compile units (objects).
Lots of errors from derived data, I assume you did an upgrade of Xcode?
Do the following steps in Xcode:
Product --> (hold down Alt key) --> Clean Build Folder
Product --> Clean Project
Window --> Organizer --> Delete derived data
Then build your project again.