new c++ error with file - c++

I am 100% new at c++ so bear with me :)
I am getting an error with this file and not sure why. any help is appreciated.
#include <iostream>
using namespace std;
int main()
{
cout << "hi" << endl;
return 0;
}
------------ Build: Debug in 1600 (compiler: GNU GCC Compiler)-------------
g++ -o bin/Debug/1600 obj/Debug/main.o obj/Debug/src/test.o obj/Debug/test03.o
duplicate symbol _main in:
obj/Debug/main.o
obj/Debug/test03.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

From the source files symbols are created. main in the .cpp file becomes _main as a symbol. During linking there can be only one main function, hence only one _main symbol is expected and allowed.
You have three object files that were created:
obj/Debug/main.o which contains main
obj/Debug/src/test.o
obj/Debug/test03.o which also contains main
Probably because you have a .cpp file for each of them and the command line or IDE you are using asked for them all to be compiled.
duplicate symbol _main
The text above is telling you that the linker (trying to make sense of all the compiled object (.o) files) found more than one main.
So the solution is to look at your IDE settings and remove the other files (or at least remove main from the other files) because you are only interested in compiling the one source file.

Its hard to tell what you're running from the question.
Here is how to build a simple C++ program using gcc
In
my_program.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "hi" << endl;
return 0;
}
To compile to object files type
g++ -c my_program.cpp
To link (you'd normally have more files here)
g++ -o my_program my_program.o
So, this isn't very fun so most people use a build system like make, cmake, msbuild or whatever the CLion IDE uses.

Related

Undefined symbols for architecture arm for

I am new to C++, and trying to use get_string, but I am not sure what I writing wrong that is creating an error.
The code I have is the following:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
string name = get_string("What's your name? ");
printf("hello, %s\n", name);
}
and it keeps saying the following error:
Undefined symbols for architecture arm64:
"_get_string", referenced from:
_main in hello-890d43.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1
Does any one know what I am doing wrong?
I expected the code take an input and print out hello, (your input).
The same thing happened to me when trying to use the cs50 C library on my local Mac with M1 chip.
Even after installing the library as described here: https://cs50.readthedocs.io/libraries/cs50/c/
It still didn't work.
The problem is coming from the fact that the make command here doesn't include the link command by default.
Assuming your source file is named hello.c instead of just doing:
make hello
You have to enter the following in your terminal in order to compile:
clang hello.c -o hello -lcs50
Afterwards, when running the executable, it works as expected:
./hello
make hello LDLIBS="-lcs50"
LDLIBS to include the cs50 library

C++ files stopped compiling - keep getting error: linker command failed with exit code 1 (use -v to see invocation)

I've had no problems compiling in the past, and code that I wrote previously is compiling fine, but it is now giving this error when I try to compile a very simple C++ file using Visual Studio Code on my M1 Mac:
Code:
#include<iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
The terminal commands I've tried (all give same error):
g++ -std=c++14 test.cpp -o test
g++ -std=c++11 test.cpp -o test
g++ test.cpp -o test
Error:
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)
I've tried to delete and re-create the file with different names, restarting VSC, using different output file names and nothing changes this.
This was occurring as I hadn't saved the file prior to compiling and therefore the compiler couldn't find it.

C++ Error on Neural Network Implementation: duplicate symbols for architecture x86_64

Hey guys I'm trying to implement the backpropagation algorithm in C++ and I've tried Xcode and C++ Eclipse but I'm getting the same error and I have no idea how to fix it, I've tried searching here but none of the proposed solution worked, here is the following error message and my code.
Error Message:
make all
Building file: ../src/NeuralNet.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/NeuralNet.d" -MT"src/NeuralNet.d" -o"src/NeuralNet.o" "../src/NeuralNet.cpp"
Finished building: ../src/NeuralNet.cpp
Building target: NeuralNet
Invoking: MacOS X C++ Linker
g++ -o "NeuralNet" ./src/Net.o ./src/NeuralNet.o
duplicate symbol __ZN3NetC2ERKNSt3__16vectorIjNS0_9allocatorIjEEEE in:
./src/Net.o
./src/NeuralNet.o
duplicate symbol __ZN3NetC1ERKNSt3__16vectorIjNS0_9allocatorIjEEEE in:
./src/Net.o
./src/NeuralNet.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [NeuralNet] Error 1
And here is my code:
NeuralNet.cpp
#include <iostream>
#include <vector>
#include "Net.cpp"
using namespace std;
int main() {
vector<unsigned> topology;
topology.push_back(10);
Net net(topology);
return 0;
}
Net.h
#ifndef NET_H_
#define NET_H_
#include <vector>
using namespace std;
class Net {
public:
Net(const vector<unsigned>& topology);
};
#endif
Net.cpp
#include "Net.h"
Net::Net(const vector<unsigned>& topology) {
// TODO Auto-generated constructor stub
}
Your IDE is trying to compile both .cpp files as individual compilation units, because usually those contain the individual compilation units.
However you are including Net.cpp in NeuralNet.cpp, so the code in it (i.e. the implementation of Net::Net) is compiled in the unit of Net.cpp as well as the unit of NeuralNet.cpp.
After compiling both compilation units the linker is called to link them together. It notices that Net::Net appears twice and doesn't know which to choose, therefore the error.
You should never #include a .cpp file. Replace #include "Net.cpp" with #include "Net.h". You always include the header (.h) for the class, not the source (.cpp).

PostgreSQL external C function link failed on Mac OSX

I'm trying to build an external PostgreSQL function on OSX 10.11 with both clang and gcc, but link failed with the following errors:
c++ -I/usr/local/Cellar/postgresql/9.5.3/include/server -fpic -c ./main.c
c++ -shared -o ttt.dylib main.o
Undefined symbols for architecture x86_64:
"_deconstruct_array", referenced from:
_psql_nearest in main.o
"_elog_finish", referenced from:
_psql_nearest in main.o
"_elog_start", referenced from:
_psql_nearest in main.o
"_get_typlenbyvalalign", referenced from:
_psql_nearest in main.o
"_pfree", referenced from:
_psql_nearest in main.o
"_pg_detoast_datum", referenced from:
_psql_nearest 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)
It looks like I need to link my library with some of PostgreSQL libraries. What are these libraries?
main.cpp:
extern "C" {
#include <postgres.h>
#include <fmgr.h>
#include <utils/array.h>
#include <utils/lsyscache.h>
#include <catalog/pg_type.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(psql_nearest);
Datum psql_nearest(PG_FUNCTION_ARGS) {
if(PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
elog(ERROR, "DOC2VEC: NULL INPUT DATA");
PG_RETURN_NULL();
}
ArrayType *_docVector = PG_GETARG_ARRAYTYPE_P(0);
Oid elTypeVals = ARR_ELEMTYPE(_docVector);
if (elTypeVals != FLOAT4OID) {
elog(ERROR, "DOC2VEC: INVALID INPUT DATA TYPE");
PG_RETURN_NULL();
}
int16 typeLenVals = 0;
bool typeByValVals = false;
char typeAlignVals = char(0);
get_typlenbyvalalign(elTypeVals, &typeLenVals, &typeByValVals, &typeAlignVals);
Datum *inputVals;
bool *nullVals;
int nVals;
deconstruct_array(_docVector, elTypeVals, typeLenVals, typeByValVals, typeAlignVals, &inputVals, &nullVals, &nVals);
pfree(inputVals);
pfree(nullVals);
PG_RETURN_NULL();
}
}
Thanks to PostgreSQL developers, they explained me the difference in Linux and OSX linking of external functions.
Instead of -shared you need -bundle -bundle_loader /path/to/postgres,
and there are some other linker flags that are advisable too.
Also, PostgreSQL expects the file extension for loadable modules to be .so even on OSX.
It's usually better to use PGXS to build extensions, instead of
learning such details for yourself.
Or you can crib from one of the extensions in the contrib/
source tree.
If you need to link, you will need the -L flag to point the linker to the path where the postgres libraries are located (the linker equivalent of the -I compiler flag). and the -l flag to actually link the libraries (one for each library); the library name without the lib prefix and without the extension.
In your case, something along the lines of -L/usr/local/Cellar/postgresql/9.5.3/lib -lpostgres
(There's a variety of library files in that directory; try -lpg to start with.
The reference to _pfree in your error message also suggest to link pgcommon, which contains the implementation of pgree (at least when using nm libpgcommon.a).
)
You may want to read up a bit more on compiling and linking in general; you do the right thing for compiling with the -I flag, but oddly then miss out on the linking step. And learning about make and Makefiles will come in handy.
I also don't understand the extern "C" { part for a .c file, which is clearly a C-only file. extern "C" is usually used in C++ files for compatibility with C.

Compile error: Undefined symbols: "_main", referenced from: start in crt1.10.5.o

I have the following code:
#include <iostream>
using namespace std;
class testing{
int test() const;
int test1(const testing& test2);
};
int testing::test() const{
return 1;
}
int testing::test1(const testing& test2){
test2.test();
return 1;
}
after compilation, it gives me the following error:
Undefined symbols:
"_main", referenced from:
start in crt1.10.5.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Why is it complaining about main? Can't i declare main in another file and include this one?
Thanks a lot!
You have tried to link it already:
g++ file.cpp
That will not only compile it, but try to already create the executable. The linker then is unable to find the main function that it needs. Well, do it like this:
g++ -c file.cpp
g++ -c hasmain.cpp
That will create two files file.o and hasmain.o, both only compiled so far. Now you can link them together with g++:
g++ -omy_program hasmain.o file.o
It will automatically figure out that those are files already compiled, and invoke the linker on them to create a file "my_program" which is your executable.
If you declare the main function in another file, then you must compile the two files separately, and then link them into 1 executable.
Unless you include the entire contents of the file from the file with the main function, that will work too, though a bit odd. But, if you do this then you have to make sure that you compile the file which has the main() function.
Try these (they worked for me):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
brew install mingw-w64
Please save your code before running.
For reference refer to this video