I am trying dynamic-linking test.
mylib.cpp
#include<iostream>
#include<stdio.h>
using namespace std;
int hello(){
cout<<"Hello,World!"<<endl;
return 1;
}
then compile
g++ -shared -o libmylib.so mylib.cpp
Now there appears the libmylib.so
test.cpp
#include <iostream>
int hello();
int main() {
hello();
std::cout << "Test Finish!\n";
return 0;
}
Try to compile with this,
g++ -o test test.cpp -L ./
There comes the error
Undefined symbols for architecture x86_64:
"hello()", referenced from:
_main in test-37bd2a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Do I need to add some options or are there something wrong in my source code??
Thank you for your help.
Do I need to add some options
yes, link with the library.
g++ ... -lmylib
Related
I'm trying to do a test of basic linking for cpp files, I've been searching all over and am having a lot of trouble trying to find a solution. I understand that I have to include the header in both cpp's, but I'm having trouble trying to run these two together.
//testMain.cpp
#include <iostream>
#include <stdio.h>
#include "func.h"
using namespace Temp;
int main()
{
getInfo();
return 0;
}
//func.h
#ifndef FUNC_H
#define FUNC_H
#include <iostream>
#include <stdio.h>
namespace Temp{
int getInfo();
}
#endif
//functions.cpp
#include "func.h"
using namespace std;
int Temp::getInfo()
{
return 5 + 6;
}
//error that I'm getting using VS Code
cd "/Users/jcbwlsn/Downloads/Coding/CPP/Workspace/RPG Project/src/" && g++ testMain.cpp -o testMain && "/Users/jcbwlsn/Downloads/Coding/CPP/Workspace/RPG Project/src/"testMain
Undefined symbols for architecture x86_64:
"Temp::getInfo()", referenced from:
_main in testMain-1f71a1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You're supposed to specify all translation unit files when linking a C++ program.
Your program consists of two source files, testMain.cpp and functions.cpp.
Hence the compile-and-link command should be something like:
g++ testMain.cpp functions.cpp -o testMain
Alternatively you can compile each source into separately and then link them into an executable:
g++ -c testMain.cpp -o testMain.o
g++ -c functions.cpp -o functions.o
g++ testMain.o functions.o -o testMain
Having some kind of a Makefile helps to automate this.
I'm new in cpp and when I compile two cpp files in terminal and shows the error as followed
Undefined symbols for architecture x86_64:
"func(int)", referenced from:
_main in main-45889e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
here are my codes:
main.cpp
#include <stdio.h>
#include <iostream>
#include "func.hpp"
int main(int argc, char* args[]){
func(1);
return 0;
}
func.cpp
#include "func.hpp"
#include <iostream>
int func(int a){
std::cout<<a<<std::endl;
}
func.hpp
int func(int a);
it succeed when I use Xcode to run the code while failed when using terminal to compile with the following g++ commands:
g++ -c main.cpp
g++ -c func.cpp
g++ main.o func.o -o main.out
Please help
I am trying to compile a c++ program which works fine in Xcode but gives error in terminal.
main.cpp
int main(int argc, const char * argv[])
{
Example* example =new Example();
example->show();
}
example.h
class Example {
public:
void show();
};
example.cpp
void Example::show() {
std::cout<<"Hello World"<<std::endl;
}
Error i get
"Example::show()", referenced from:
_main in cckpIa3V.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I am compiling using g++
g++ -o test main.cpp
You aren't linking in example.o. You don't show your command line/Makefile, so this is (roughly) what you need to type:
$ g++ -o example main.cpp example.cpp
That will compile and link the source files to an executable called example.
main.cpp
#include <iostream>
#include "Burrito.h"
using namespace std;
int main(){
Burrito b;
return 0;
}
Burrito.h
#ifndef BURRITO_H
#define BURRITO_H
class Burrito{
public:
Burrito();
};
#endif
Burrito.cpp
#include "Burrito.h"
#include <iostream>
Burrito::Burrito(){
}
Compile & Link :
lzsb$ g++ main.cpp -o main
Undefined symbols for architecture x86_64:
"Burrito::Burrito()", referenced from:
_main in ccVpCr0z.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
lzsb$
Platform:
Mac OS 10.6.8
G++ : i686-apple-darwin10 --with-gxx-include-dir=/usr/include/c++/4.2.1
You need to compile the Burrito.cpp file as well. The compiler creates object files from each .cpp file and links them afterwards. This is where your call fails, because the linker can't find the referenced Burrito class in any of your object files. To fix your compiler call just add Burrito.cpp
g++ main.cpp Burrito.cpp -o main
Your compile line should be:
g++ Burrito.cpp main.cpp -o main
I have this code:
#include <iostream>
#include <mp4.h>
int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
MP4Read("filename", MP4_DETAILS_ALL );
return 0;
}
And i've added -I/opt/local/include and -L/opt/local/lib to the path (where the mp4 library resides after installing it through macports), but all i get is:
Undefined symbols: "_MP4Read",
referenced from:
_main in main.o ld: symbol(s) not found
Even though XCode finds it and autocompletes properly...
You need to link the library most likely, i.e. add -lmp4 or similar to your linking commands.
You have only specified the paths. You need to link in the mp4 library. Something like the following:
g++ -I /.../ -L /.../ -lmp4 -o out main.cpp
The -L flags tell the compiler where to look, the -l flag tells it what to look for.