I have got in the same directory 3 files:
hellomake.cu
#include<hellofunc.h>
int main(){
myPrintHelloMake();
return 0;
}
hellofunc.c
#include<stdio.h>
#include<stdlib.h>
void myPrintHelloMake(void){
printf("Hello dummy!\n");
return;
}
hellofunc.h
void myPrintHelloMake(void)
Makefile
CC=/usr/local/cuda-5.5/bin/nvcc
CFLAGS=-I.
hellomake: hellomake.cu hellofunc.c
$(CC) -o hellomake hellomake.cu hellofunc.c -I.
But when I run through terminal make it prints out:
/usr/local/cuda-5.5/bin/nvcc -o hellomake hellomake.cu hellofunc.c -I.
/tmp/tmpxft_000013bf_00000000-14_hellomake.o: In function main':
tmpxft_000013bf_00000000-3_hellomake.cudafe1.cpp:(.text+0x5): undefined reference tomyPrintHelloMake()'
collect2: ld returned 1 exit status
make: * [hellomake] Error 1
What might be the problem?
You could change the filename hellofunc.c to hellofunc.cpp.
If the filename cannot be changed, you could search more information about how to invoke C functions in C++ code.
You should change the name of hellofunc.c to hellofunc.cu. You may also use #include "hellofunc.h".
Related
I'm trying to use the g_struct variable that's defined in struct.cpp and declared in struct.hpp inside the test.cpp, but the linking fails. Why is that?
// test.cpp
#include "struct.hpp"
int main(void)
{
g_struct.a = 1;
return 0;
}
// struct.cpp
#include "struct.hpp"
Struct g_struct;
// struct.hpp
#pragma once
struct Struct {
int a;
};
extern Struct g_struct;
# Makefile
CC = g++
all: struct.o test
test: test.cpp
$(CC) -o test test.cpp
struct.o: struct.cpp
$(CC) -c -o struct.o struct.cpp
.PHONY: clean
clean:
rm -f *.o test
Linking error:
$ make
g++ -c -o struct.o struct.cpp
g++ -o test test.cpp
/tmp/ccgSoJhd.o: In function `main':
test.cpp:(.text+0xd): undefined reference to `g_struct'
collect2: error: ld returned 1 exit status
Makefile:6: recipe for target 'test' failed
make: *** [test] Error 1
If I link without the -c flag, I get:
$ make
g++ -o struct.o struct.cpp
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:9: recipe for target 'struct.o' failed
make: *** [struct.o] Error 1
You have to link struct.o, which has Struct g_struct;.
test: test.cpp
$(CC) -o test test.cpp struct.o
I write simple testing program in C++, which will tell Hello, Alex and exit.
Here it's code:
main.cpp:
#include <iostream>
#include <dlfcn.h>
int main()
{
void* descriptor = dlopen("dll.so", RTLD_LAZY);
std::string (*fun)(const std::string name) = (std::string (*)(const std::string)) dlsym(descriptor, "sayHello");
std::cout << fun("Alex") << std::endl;
dlclose(descriptor);
return 0;
}
dll.h:
#ifndef UNTITLED_DLL_H
#define UNTITLED_DLL_H
#include <string>
std::string sayHello(const std::string name);
#endif
dll.cpp:
#include "dll.h"
std::string sayHello(const std::string name)
{
return ("Hello, " + name);
}
makefile:
build_all : main dll.so
main : main.cpp
$(CXX) -c main.cpp
$(CXX) -o main main.o -ldl
dll.so : dll.h dll.cpp
$(CXX) -c dll.cpp
$(CXX) -shared -o dll dll.o
But when I build my code with make, I have such error:
/usr/bin/ld: dll.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
dll.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
makefile:8: recipe for target 'dll.so' failed
make: *** [dll.so] Error 1
What did I make not correct?
P.S. I use GNU Make 3.81 on Ubuntu Server 14.04.3 with GNU GCC 4.8.4
Update
If I link dll.so file with -fPIC param, I have the same error
Firstly, a bit off topic, but in your makefile, it would be better to specify build_all as a phony target
.PHONY: build_all
Next, you are compiling dll.cpp without relocatable code. You need to add -fpic or -fPIC (see here for an explanation of the difference).
$(CXX) -c dll.cpp -fpic
Lastly, unix doesn't automatically add file suffixes, so here you need to specify .so:
$(CXX) -shared -o dll.so dll.o
Why is uuid_generate_random undefined? As far as I know I am including the uuid library when compiling.
Any advice whats going wrong?
me#me-vm:~/Projects/_Tests/test_cba$ make
g++ -o res main.cpp -Luuid -std=c++11
/tmp/ccRubbJa.o: In function `main':
main.cpp:(.text+0x30): undefined reference to 'uuid_generate_random'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
Simple code:
#include <iostream>
#include <uuid/uuid.h>
int main(int argc, char** argv) {
uuid_t id;
uuid_generate_random(id);
return 0;
}
Using wrong command.Use below
g++ -o res main.cpp -luuid -std=c++11
-L is used to provide location of library
-l is used to provide name library.
I am trying to define a macro for the source file from the command line on an ubuntu system using the -D flag .
The source file is:
#include<iostream>
using namespace std;
int factorial(int n){
if(n!=1){
return(n * factorial(n-1));
}
else return 1;
#ifdef DEEPAK
cout<<"hello"<<endl;
#endif
}
int main()
{
factorial(4);
return(0);
}
The command I am typing is:
gcc -Wall -DDEEPAK factorial.cpp -o main
BUT, I am getting the error:
/tmp/cc4Ii5l2.o: In function `__static_initialization_and_destruction_0(int, int)':
factorial.cpp:(.text+0x63): undefined reference to `std::ios_base::Init::Init()'
factorial.cpp:(.text+0x72): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
Any help would be deeply appreciated. Thanks.
You should use g++ instead of gcc in the command, because gcc doesn't link to the C++ STL by default, and hence it gives an undefined reference to std::ios_base.
g++ -Wall -DDEEPAK factorial.cpp -o main
Possible dup of this or this, but even after trying to dig through the answers for a while, I couldn't resolve this.
While trying to compile following makefile,
all: test
test: constants.h Point.h Point.cpp line_t.h line_t.cpp drawing_t.h drawing_t.cpp clipper_t.h clipper_t.cpp main.cpp
g++ -o test Point.cpp line_t.cpp drawing_t.cpp clipper_t.cpp main.cpp -lglut
I get an error:
g++ -o test Point.cpp line_t.cpp drawing_t.cpp clipper_t.cpp main.cpp
-lglut /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function _start': (.text+0x18): undefined reference tomain'
collect2: ld returned 1 exit status make: *** [test] Error 1
I am new at Makefile. I guess, I am missing something too obvious.
Apparently none your files define a function with the signature
int main();
or
int main(int argc, char *argv[]);