C++ ERROR multiple definition of function 2 - c++

I have a "multiple definition of function" error on f2 function. The same error is in CodeBlocks and MS Visual Studio 2008.
What changes need for this code to work without errors?
Here is the simple example of my program? that has error:
**main.cpp**
#include <iostream>
#include"head.h"
using namespace std;
int main()
{
int n = 5;
cout<<f1(n)<<endl;
cout<<f2(n)<<endl;
return 0;
}
**head.h**
#ifndef HEAD_INCLUDED
#define HEAD_INCLUDED
#include "func1.cpp"
#include "func2.cpp"
int f1(int);
int f2(int);
#endif // HEAD_INCLUDED
**func1.cpp**
#include <iostream>
using namespace std;
int f1(int a)
{
return a+a;
}
**func2.cpp**
#include <iostream>
using namespace std;
int f1(int a)
{
return a+a;
}
-------------- Build: Debug in test (compiler: GNU GCC Compiler)---------------
g++ -Wall -fexceptions -g -c /home/laptop/Documents/CodeBlocksProjects/test/func2.cpp -o obj/Debug/func2.o
g++ -Wall -fexceptions -g -c /home/laptop/Documents/CodeBlocksProjects/test/main.cpp -o obj/Debug/main.o
g++ -o bin/Debug/test obj/Debug/func2.o obj/Debug/main.o
obj/Debug/main.o: In function f2(int)':
/home/laptop/Documents/CodeBlocksProjects/test/func2.cpp:5: multiple definition off2(int)'
obj/Debug/func2.o:/home/laptop/Documents/CodeBlocksProjects/test/func2.cpp:5: first defined here
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
2 error(s), 0 warning(s) (0 minute(s), 1 second(s))

Try this:
head.h
#ifndef HEAD_INCLUDED
#define HEAD_INCLUDED
int f1(int);
int f2(int);
#endif // HEAD_INCLUDE
head.cpp
#include "head.h"
int f1(int a)
{
return a+a;
}
int f2(int a)
{
return a+a;
}
test1.cpp
#include <iostream>
#include "head.h"
using namespace std;
int main(){
int n = 5;
int val1 = f1(3);
int val2 = f2(5);
cout << "val1 = " << val1 << endl;
cout << "val2 = " << val2 << endl;
return 0;
}
compile and execute commands in linux
g++ -o test1.o head.cpp test1.cpp
./test1.o

Related

Error: "ld returned 1 exit status" when building DLL file in C++

I'm just learning how to make a DLL in C++ and I made a DLL and when I try to compile my main Cpp file it gives this error:
id.exe: cannot find -lfactorial
collect2.exe: error: id returned 1 exit status
This is the file that my DLL was built with:
extern "C" __declspec(dllexport) long int factorial(int);
extern "C" __declspec(dllexport)
long int factorial( int n ){
if( n > 1 )
return n * factorial( n - 1 );
else return 1;
}
I compiled it with this command:
g++ factorial.cpp -o factorial.dll -shared
This is the original code:
#include <iostream>
using namespace std;
extern "C" __declspec(dllexport) long int factorial(int);
int main(){
int n;
cout<<"Enter n: ";
cin>>n;
system("cls");
cout<<"Factorial "<<n<<" = "<<factorial(n);
return 0;
}
And I compiled the original code with this command:
g++ main.cpp -o app.exe -lfactorial
Thank you for telling me where the problem is

Unable to hook ncurses functions with LD_PRELOAD

I'm trying to hook some ncurses functions but they don't have any effect.
ncurses isn't statically linked, so I don't see why it wouldn't work.
test.cpp
#include <cstdio>
#include <cstdlib>
#include <curses.h>
int main() {
initscr();
cbreak();
noecho();
getch();
endwin();
return 0;
}
Compiled with: gcc test.cpp -o test -std=c++11 -lncurses
hook.cpp
#include <dlfcn.h>
#include <cstdio>
#include <cstdlib>
int getch() {
typedef int getch ();
getch* old_getch = (getch*) dlsym(RTLD_NEXT, "getch");
int result = old_getch();
fprintf(stderr, "getch() = %i\n", result);
return result;
}
int noecho() {
typedef int noecho ();
noecho* old_noecho = (noecho*) dlsym(RTLD_NEXT, "noecho");
int result = old_noecho();
fprintf(stderr, "noecho() = %i\n", result);
return result;
}
int endwin() {
typedef int endwin ();
endwin* old_endwin = (endwin*) dlsym(RTLD_NEXT, "endwin");
int result = old_endwin();
printf("endwin called");
return result;
}
Compiled with: gcc hook.cpp -o hook.so -shared -ldl -fPIC -std=c++11
It sadly outputs nothing, and I'm completely stumped.
The specification doesn't state getch has to be a function (not a macro). Actually, in ncurses-6.1, getch defined as
#define getch() wgetch(stdscr)
Nonetheless, there is a getch function in libncurses (which simply calls wgetch(stdscr)), so dlsym(libncurses_handle,"getch") does work.

LLVM run PassManager (non-legacy)

How do I run a non-legacy PassManager? I have tried doing the following but there is some exception thrown when trying to invalidate the analysis manager in the run function. Is there something else I should do for initialization?
llvm::AnalysisManager<Module> mm;
PassBuilder builder;
auto pm = builder.buildModuleOptimizationPipeline(PassBuilder::OptimizationLevel::O3);
pm.run(module, mm );
These snippets illustrate how to run and setup to run modern custom function and module pass on some .c/.cpp file... complete with a makefile. This works for LLVM 6 which is pretty recent (march 2018). It does not use the legacy pass manager.
HelloWorld.cpp:
#include <llvm/Pass.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Module.h>
#include <llvm/Support/raw_ostream.h>
namespace {
struct Hello : public llvm::FunctionPass {
static char ID;
Hello() : llvm::FunctionPass{ID} {}
bool runOnFunction(llvm::Function &F) override {
llvm::errs() << "Hello ";
llvm::errs().write_escaped(F.getName()) << "\n";
return false;
}
};
struct Hello2 : public llvm::ModulePass {
static char ID;
Hello2() : llvm::ModulePass{ID} {}
bool runOnModule(llvm::Module &M) override {
llvm::errs() << "Name of the module ", llvm::errs().write_escaped(M.getName()) << "\n";
for(auto iter = M.getFunctionList().begin(); iter != M.getFunctionList().end(); ++iter) {
llvm::errs() << "Function name:" << iter->getName() << "\n";
}
return false;
}
};
}
char Hello::ID = 0;
static llvm::RegisterPass<Hello> X("Hello",
"Hello World Pass",
false,
false
);
char Hello2::ID = 1;
static llvm::RegisterPass<Hello2> Y("Hello2",
"Hello World2 pass",
false,
false
);
Corresponding makefile:
LLVM_VERSION=
LLVM_INCLUDEDIR = `llvm-config-6.0 --includedir`
LLVM_FLAGS = `llvm-config-6.0 --cxxflags --ldflags --system-libs --libs all`
CXX = clang++-6.0
CXXFLAGS = -g -std=c++11 -O3 -I $(LLVM_INCLUDEDIR) -I $(LLVM_INCLUDEDIR)
Hello.so:
$(CXX) -fPIC $(CXXFLAGS) HelloWorld.cpp $(LLVM_FLAGS) -shared -o Hello.so
Hello: Hello.so
testfile:
clang++-6.0 -emit-llvm -c test.cpp -o test.bc
runFunctionPassOnTestFile: Hello testfile
opt-6.0 -load ./Hello.so -Hello < test.bc > /dev/null
runModulePassOnTestfile: Hello testfile
opt-6.0 -load ./Hello.so -Hello2 < test.bc > /dev/null
clean:
rm *.o *.so *.out *~
DBG:
#echo LLVM INCLUDE DIRS $(LLVM_INCLUDEDIR) $(test)
A simple file to test everything on, test.cpp:
#include <stdio.h>
#include <stdlib.h>
int a = 4;
int c = 5;
int d = 6;
int e = 7;
int bar() { int *a = (int*) malloc(4); e = 1; return 1;}
int foo() { return 2; }
int barfoo() { return 3; }
int main() {
printf("Testing testing\n");
return 0;
}

My file can compile but I get "no such file or directory" when I try to run it

I figured that the problem was that I was not using ./a.out instead of ./filename, but that wasn't the case.
This is how I compile my program:
g++ -o -Wall -pthread filename.cpp
Running:
./filename
I figured that running pthread programs would be different than running a standard c++ program, but that wasn't the case.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct argStruct {
int arg1;
int arg2;
};
void *sum(void *arguments) {
struct argStruct *args = (struct argStruct *)arguments;
int a = args -> arg1;
int b = args -> arg2;
int c = a + b;
printf("%d + %d = %d ",a,b,c);
pthread_exit(NULL);
}
int main() {
pthread_t thr1, thr2;
struct argStruct args;
args.arg1 = 3;
args.arg2 = 10;
int t1, t2;
pthread_create(&thr1, NULL, &sum, (void *)&args);
pthread_create(&thr2, NULL, &sum, (void *)&args);
pthread_join(thr1, NULL);
pthread_join(thr2, NULL);
exit(EXIT_SUCCESS);
}
By calling
g++ -o -Wall -pthread filename.cpp
You instruct the compiler to write the result to a file named -Wall.
You therefore want to use one of these
g++ -Wall -pthread filename.cpp
g++ -Wall -pthread -o filename filename.cpp
The first writes the result to a.out, the second to filename. (Also it enables the warnings)

Huge ammount of errors during compilation of program with Magick++

During the compilation of a program I was making, that uses the Magick++ library, there appeared an incredibily huge ammount of errors.
This is the command line I used for compile it (g++'s version is 4.9.2, running on Ubuntu 12.04):
g++ -std=c++14 -g -fopenmp `Magick++-config --cppflags --cxxflags` main.cpp -o glitch_img2 `Magick++-config --ldflags --libs`
Here is the program's code:
#include <iostream>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cmath>
#include <string>
#ifdef __OPENMP
#include <omp.h>
#endif
#include <Magick++.h>
double d_max(double a,double b)
{
return (a>b) ? a : b;
}
int ui_clip(int x,int a,int b)
{
return (x<b) ? b : ((x>a) ? a : x);
}
int transform(int x)
{
return ((x*32+1654)%21+x*11+642)%164%33%16;
}
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
{
InitializeMagick(*argv);
Image img1;
img1.read(argv[1]);
int size_x = img1.columns();
int size_y = img1.rows();
int np = size_x*size_y;
Image out;
out.size(Geometry(size_x,size_y));
double *buff_img1 = new double[3*size_x*size_y];
double *buff_out = new double[3*size_x*size_y];
img1.write(0,0,size_x,size_y,"RGB",DoublePixel,buff_img1);
int ix, iy;
#ifdef __OPENMP
#pragma omp for private(ix,iy) schedule(static)
#endif
for (iy=0;iy<size_y;iy++)
{
for (ix=0;ix<size_x;ix++)
{
int loc = (size_x*iy+ix)*3;
double pimg1[3] = {
buff_img1[3*loc ],
buff_img1[3*loc+1],
buff_img1[3*loc+2]
};
int x1 = (ix*(1+transform(ix)))%size_x;
int y1 = (iy*(1+transform(iy)))%size_y;
int loc1 = ui_clip((size_x*y1+x1)%np,0,np-1)*3;
double out_pix[3] = {
buff_img1[3*loc1 ],
buff_img1[3*loc1+1],
buff_img1[3*loc1+2]
};
buff_out[3*loc ] = abs(out_pix[0]-pimg1[0]);
buff_out[3*loc+1] = abs(out_pix[1]-pimg1[1]);
buff_out[3*loc+2] = abs(out_pix[2]-pimg1[2]);
}
}
out.read(size_x,size_y,"RGB",DoublePixel,buff_out);
out.write("Output.png");
}
And those errors are posted here.
What causes such errors and how can they be fixed?
EDIT: The mentioned behaviour dissapears when I remove the Magick++.h header.