c++ can't compile a file - c++

I'm a beginner at c++ and I'm trying to write a program to find greatest common factor. In main i have:
#include <iostream>
#include <cstdlib>
#include "longgcd.cpp"
int main(int argc, char* argv[]){
long gcd(long m, long n);
long m,n,g;
m=atol(argv[1]);
n=atol(argv[2]);
g=gcd(m,n);
std::cout<<"gcd("<<m<<","<<n<<")="<<g<<std::endl;
return 0;
}
and then i put the subfunction into another file called longgcd.cpp
#include <iostream>
#include <cstdlib>
long gcd( long m, long n){
long r;
while(n !=0){
r=m%n;
m=n;
n=r;
}
return m;
}
somehow longgcd.cpp can't compile. i get an error:
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [longgcd] Error 1
somehow I have difficulty running this program and making it work, i can't see whats wrong with it. Thanks for any help or suggestions.

Can you show the line you used to compile? It sounds like you tried to compile longgcd.cpp independently as an executable, and since that file doesn't have main, the linker correctly complained that it couldn't find main.
The simplest solution is to compile both files together
g++ $FLAGS longgcd.cpp main.cpp

You should be compiling the source file that contains the main() function.
Note that #includeing cpp's is generally discouraged. You can put the declaration for gcd in a header file and include this file from both the implementation cpp containing the code for it and the main file that calls it. In this case you will need to specify both cpp files to the compiler command line because they're both needed to assemble the final program. Even with this complication this way is much better than including cpps.

One issue is that main should be in your .cpp file, and not your header.
Another is that you normally #include a header (.h or .hpp) into a .cpp file and not the other way around.
Also please get a decent C++ book to read.

Do not include longgcd.cpp. You should almost never include a .cpp (unless you really, really, know what is going down)
You should specify all the cpps to the compiler. E.g: g++ main.cpp longgcd.cpp
Also move the long gcd(long m, long n); line above your main function.

Related

FPC: file not recognized: file format not recognized

I have something that need to link some C++ codes to main Pascal program. I followed this tutorial, now I have:
download.h
#include <iostream>
#include <curl/curl.h>
using namespace std;
// function goes here
int downloadsrc(char* pkg_name){
// do stuff
}
download.pas:
unit download;
{$link download.obj} // fpc may overwrite download.o
Interface
uses ctypes;
function downloadsrc(pkg_name:string):integer;
Implementation
// I leave this empty
end.
My main program:
program myprog;
uses
warn, download, test;
var i:integer;
begin
if ParamCount = 0 then help()
else for i:= 1 to ParamCount do
begin
if ParamStr(i) = 'download' then downloadsrc(ParamStr(i+1))
else if ParamStr(i) = 'test' then test();
end;
end.
I tried g++ to compile .h file, change the output.. but when build the program FPC says that:
download.obj: file not recognized: file format not recognized
Compiling download.pas manually still works.
Am I do something wrong here? Am I need to do some other things, like add a compile flag or modify the code?
FPC, like GCC, uses COFF objects. Make sure your .obj is COFF not OMF.
If you fix that, you probably must also link some C++ runtime library, and declare downloadsrc in a C callable way, and alter the FPC declaration to match (cdecl)

My Class.cpp is not being seen by the linker

I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and 'C++' extensions.
main.cpp
#include "Fan.h"
int main() {
Fan fan1;
return 0;
}
Fan.h
#ifndef FAN_H
#define FAN_H
class Fan {
public:
Fan();
};
#endif
Fan.cpp
#include "Fan.h"
#include <iostream>
using namespace std;
Fan::Fan() {
cout << "Fan Class" << endl;
}
I really can't seem to find anything popping out as obviously wrong. I am wondering if it is a setup problem with VS Code.
If I change #include "Fan.h" in main.cpp to "Fan.cpp" it works so it makes me think that the code works but that the linker is not setup right.
Would appreciate any help!
EDIT: Ok so I've tried the code in a different IDE and it worked. It's something to do with VS Code. Here is the error:
[Running] cd "c:\Users\<USER>\Desktop\Fan\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\<USER>\Desktop\Fan\"tempCodeRunnerFile
C:\Users\<USER>\AppData\Local\Temp\cclMFKxO.o:tempCodeRunnerFile.cpp:(.text+0x57): undefined reference to `Fan::Fan()'
collect2.exe: error: ld returned 1 exit status
It sounds like the IDE is only compiling main.cpp. You need to find the command that is compiling main.cpp and ensure that it also compiles fan.cpp into fan.obj. You will also need to ensure that both main.obj and fan.obj are passed to the linker (which produces the executable program, main.exe or whatever).
There are two steps involved here:
cpp -> obj (compiling each source file into a matching object file)
obj -> exe (linking many object files into an executable)
I would say to make a CMakeLists.txt file and add main.cpp and fan.cpp into the add_executable section. Then VS can handle and run files through CMake.

Duplicate Symbols in object files:

I am trying to compile two .cpp files, (foo.cpp and bar.cpp) and build a shared object (project.so). But the compilation fails and (a part of) the error I am getting is:
....
duplicate symbol _n in:
foo.o
bar.o
ld: 5 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1(use -v to see
invocation)
make: *** [project.so] Error 1
ERROR: compilation failed for package ‘project’
My .cpp files have few common and uncommon header files, a few commonly and uncommonly named functions, and a set of commonly named variables:
foo.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include <cmath>
size_t m1;
double k1=2.0;
std::vector<double> x,y;
std::vector<double> z;
size_t n,p;
void inputfoo(){...}
void output(){...}
bar.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include <cmath>
#include "Eigen/Dense"
#include "Eigen/Cholesky"
size_t m2;
double k2=2.0;
std::vector<double> x,y;
std::vector<double> z;
size_t n,p;
void inputbar(){...}
void output(){...}
My attempt:
I am able to get lesser number of 'duplicate symbols' error if I differ the names of global variables in each .cpp files. That is, if I change the size_t m to size_t m1 in foo.cpp and size_t m2 in bar.cpp, i do not get this part in the error
duplicate symbol _m in:
foo.o
bar.o
So, now I can see that the 5 symbols in the errors are for x,y,z,n,p ( defined globally in each .cpp file)
Same goes if I differ the name of the commonly named functions. Previously, I would also get this part in the error,
duplicate symbol __Z4inputP4init3RNGPi in:
foo.o
bar.o
which directs me to the input(){...} function.
So, I changed the name of one of the commonly named function ( input (){..} ), to inputfoo and inputbar and the respective error went away.
Now, I am sure i will be able to compile these two successfully if i make the names unique in each file. However, I cant change the x,y,z,n,p because they are numerous in these files and I have many more files to work with which have common named functions and variables.
Can anyone please explain it to me what is happening here and how to fix it? I would really like to know what is causing this. I tried reading from previous posts, 'Understanding the origin of a linker duplicate symbol error ' but I don't think it is a header related problem.
Thank you so much.
The example is incomplete, which makes it hard to comment.
But let me make a guess: you have globals in both files and they are visible across both. That is a design error. You can either
make them local to each file if their state is not shared, use static for that
make them shared by declaring one file only and using extern in the other.
But the error you quote is different and we don't know anything about your input::init()...
Also, I also see nothing related to Rcpp here, so wht add the tag for it?

Trying to use functions in a header/cpp file

I have two files:
hello.h and hello.cpp
hello.h
#ifndef __HELLO_H__
#define __HELLO_H__
using namespace std;
void PrintMessage();
#endif
hello.cpp
#include <iostream>
#include "hello.h"
using namespace std;
void PrintMessage()
{
cout << "I want to be displayed!";
}
Now, I want to use PrintMessage() in a new .cpp file, but I keep getting an error message. This is what I'm doing:
printingmessage.cpp
#include <iostream>
#include "hello.h"
using namespace std;
int main()
{
PrintMessage();
return 0;
}
Am I just doing something blatantly wrong? I do have all of them in the same folder; I assume it has something to do with Dev-C++ (what I'm using to write/compile/run), but I can't figure it out. Anyone have any ideas?
I created a folder on my desktop, put all three files inside, and I tried to compile my printingmessage.cpp file exactly as it is. This is the error I'm getting:
[Linker error] Undefined reference to 'PrintMessage()'
i don't know dev C++ , but i would strongly advise if you do any serious coding to learn/move to the terminal and use make files, or a newer IDE such as visual studios.
heres a short script you can run save it as bash.sh
something like this
g++ hello.cpp -O2 -g -c
g++ hello.o printmessage.cpp -Wall -O2 -o print
then run it with ./print
I assume it has something to do with Dev-C++ (what I'm using to
write/compile/run), but I can't figure it out.
I guess so, too. Behind the scenes, the following things have to happen:
Both files get compiled. This creates a *.obj file for every *.cpp file, and uses the header.
The object files are linked against one another and possibly against required libraries.
Your problem lies in the “one another” part of the second step: the code compiles all right, but linking fails. The header file is irrelevant at that point. More precisely, the linker invocation for printingmessage.obj contains a reference to a function which isn't defined in that object file or any of the default libraries. Most likely the problem is due to the *.cpp files not being part of the same project. You need to create a multi-source-file project where you can link multiple object files. How you do that with Dev-C++ is probably somewhere in their manuals.

Yet another linker issue

I'm having a linking issue with a basic C++ program. No, I'm not including .cpp files!
This is what's happening.
main.cpp:
#include "header.h"
#include <iostream>
int main() {
std::cout << "Hello!";
}
header.h:
#ifndef _HEADER_H
#define _HEADER_H
class Something {
public:
printContents();
};
#endif
something.cpp:
#include "header.h"
#include <iostream>
Something::printContents() {
cout << "This class's Contents!!";
}
What's happening is that I get a compiler error going: multiple definitions of some standard C function, such as strtod:
g++ -o ... main.o
build/....main.o: In function
`strtod':
../MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdlib.h:318:
multiple definition of `strtod'
build/..something.o:...something.cpp:(.text+0x0):
first defined here collect2: ld
returned 1 exit status
If I get rid of #include <iostream> in one of the two occasions and get rid of the couts, it will compile. What's going on? I'm using g++ and NetBeans to compile.
I tried in the command line:
g++ *.h *.cpp -o program
and the same thing happened.
Please note that _HEADER_H is an illegal name in C++ user code - names beginning with the underscore and an uppercase letter are reserved for the C++ implementation. This does not normally cause noticeable problems, but when you use what may be a common name in the implementation like HEADER in this context, it well might.
Modify,
Something::printContents()
{
std::cout << "This class's Contents!!";
}
NOTE: Specify the return datatype.
One of your problems is right here:
I tried in the command line: g++ *.h
*.cpp -o program
Don't pass your header files... Try something like this:
g++ *.cpp -o program
I could not reproduce your exact problem. I get this to compile and link nicely with the following few notes:
Add a void return type to the printContents-function (So it says void printContents(); in the header and void Something::printContents() { in the implementation-file)
Use std::cout rather than just cout. cout is not defined in the scope it is used
Make sure header.h ends with a blank line
Use HEADER_H rather than _HEADER_H (like Neil Butterworth says)
I use the command line g++ main.cpp something.cpp to compile.
I see a couple of problems. You shuold define the returning value of the function
printContents()
and you must write
std::cout
if you don't write
using namespace std;
The problem was in a multi-installation of MinGW. I had one already installed, and when I got Qt on my computer, it had installed it's own MinGW. Bummer, I ported the code to my university's servers and it ran OK.
Bummer!!
Thanks everybody for the help, I will definitely follow your guidelines in the future.
Header names - no underscores
Correct return type
Real code in the forums!
Leo Bruzzaniti