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[]);
Related
I am trying to compile SFML simple code:
#include <iostream>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
using namespace std;
int main(int argc, char* argv[])
{
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("127.0.0.1", 6000);
if (status != sf::Socket::Done)
{
// error...
}
return 0;
}
All libs and dependencies are installed:
sudo apt-get install libsfml-dev
sudo apt-get build-dep libsfml
I am using two methods:
g++ -c main.cpp
g++ -o main main.o -std=c++11 -lsfml-graphics -lsfml-window -lsfml-system
g++ -c main.cpp -I/home/x64joxer/SFML-2.4.0/include
g++ -o main main.o -L/home/x64joxer/SFML-2.4.0/lib -std=c++11 -lsfml-graphics -lsfml-window -lsfml-system
But I still have the same problem:
main.o: In function `main':
main.cpp:(.text+0x27): undefined reference to `sf::TcpSocket::TcpSocket()'
main.cpp:(.text+0x38): undefined reference to `sf::IpAddress::IpAddress(char const*)'
main.cpp:(.text+0x54): undefined reference to `sf::TcpSocket::connect(sf::IpAddress const&, unsigned short, sf::Time)'
main.o: In function `sf::TcpSocket::~TcpSocket()':
main.cpp:(.text._ZN2sf9TcpSocketD2Ev[_ZN2sf9TcpSocketD5Ev]+0x30): undefined reference to `sf::Socket::~Socket()'
main.cpp:(.text._ZN2sf9TcpSocketD2Ev[_ZN2sf9TcpSocketD5Ev]+0x56): undefined reference to `sf::Socket::~Socket()'
main.o:(.rodata._ZTIN2sf9TcpSocketE[_ZTIN2sf9TcpSocketE]+0x10): undefined reference to `typeinfo for sf::Socket'
collect2: error: ld returned 1 exit status
I read many tutorials and topics at the forum but I still do not know how too fix it.
My system is Kubntu 15.
Can anyone know how to fix it?
You are linking with graphics, system and window but not with network. Did you try adding -lsfml-network ?
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
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".
I'm trying to compile the following sample code available at XERCES site:
#include <xercesc/util/PlatformUtils.hpp>
// Other include files, declarations, and non-Xerces-C++ initializations.
XERCES_CPP_NAMESPACE_USE
int main(int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
with,
g++ -g -Wall -pedantic -L/usr/lib -lxerces-c -o xercesTest xercesTest.cpp
giving me the following linking error:
/tmp/ccYIHCfR.o: In function `main':
/home/cjmv/temp/xercesTest.cpp:8: undefined reference to `xercesc_2_8::XMLUni::fgXercescDefaultLocale'
/home/cjmv/temp/xercesTest.cpp:8: undefined reference to `xercesc_2_8::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_2_8::PanicHandler*, xercesc_2_8::MemoryManager*, bool)'
/home/cjmv/temp/xercesTest.cpp:18: undefined reference to `xercesc_2_8::XMLPlatformUtils::Terminate()'
/tmp/ccYIHCfR.o:(.gcc_except_table+0x10): undefined reference to `typeinfo for xercesc_2_8::XMLException'
collect2: ld returned 1 exit status
I've installed xerces-c28 and xerces-c2-dev through aptitude on my ubuntu-server 12.04
Any help would be appreciated.
Put the library last on the command line:
g++ -g -Wall -pedantic -L/usr/lib -o xercesTest xercesTest.cpp -lxerces-c
include the lib path of xerces:
try this
g++ -I/<xerces-c 2.8.0 path>/include -c xercesTest.cpp
g++ -L/<xerces-c 2.8.0 path>/lib -lxerces-c xercesTest.o