I have C++ project in Xcode
I would like to use C library aws4c in it https://code.google.com/p/aws4c/
Here is my apn.cpp file:
#include "aws4c.h"
...
int main(int argc, char *argv[])
{
aws_init();
...
}
In Xcode it looks OK, but when I'm trying to build - get error
Undefined symbols for architecture x86_64:
"aws_init()", referenced from:
_main in apn.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What is the right way to link C library to C++ project in Xcode5?
Resolved this
added
#ifdef __cplusplus
extern "C" {
#endif
and
#ifdef __cplusplus
}
#endif
to aws4c.h file and it works
Related
While implementing class member functions in.cpp file on macOS 10.15, displaying an error:
clang: Undefined symbols for architecture x86_64:error:
"Circle::Area()", referenced from:
_main in main-4cfa92.o
"Circle::Circle(double)", referenced from:
_main in main-4cfa92.o
linker command failed with exit code 1 (use -v to see invocation)
To verrify this case, I found an example on the website to try, and it worked fine when I copied the function definitions from Circle.cpp file into Circle.h file, But when the function declaration is in Circle.h and function definition is in Circle.cpp files, respectively, an error occurs
//Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
private:
double r;//radius
public:
Circle();//constructor
Circle(double R);//The constructor
double Area();//computing area
};
#endif
//Circle.cpp
#include "Circle.h"
Circle::Circle(){
this->r=5.0;
}
Circle::Circle(double R){
this->r=R;
}
double Circle::Area(){
return 3.14*r*r;
}
//main.cpp
#include <iostream>
#include "Circle.h"
using namespace std;
int main(){
Circle c(3);
cout<<"Area="<<c.Area()<<endl;
return 0;
}
Error message:
Undefined symbols for architecture x86_64:
"Circle::Area()", referenced from:
_main in main-4cfa92.o
"Circle::Circle(double)", referenced from:
_main in main-4cfa92.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Looks like you have not included Circle.cpp in the compilation step. Make sure you include both main.cpp and Circle.cpp
I am learning to code in C++ and am working in Xcode9.1 on OS X 10.13.1. While trying to understand the use of keyword extern, I encountered the problem that the following code:
extern int foo;
#include <iostream>
int main() {
foo = 7;
std::cout << foo << std::endl;
return 0;
}
results in a linker error when run:
Undefined symbols for architecture x86_64:
"_foo", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am not sure why the linker cannot find foo despite the definition being the first line in main.
Thanks very much for looking into my problem!
The linker cannot find foo, because it's not defined anywhere. By declaring extern int foo', you're telling the linker that the definition is somewhere else. Remove extern, or define foo somewhere where the linker can find it.
Have a look at this example on Wikipedia.
Right, please bare with on this, it might be quite a long one, and one related issue was solved here (I think): CMake make[2]: *** No rule to make target `/path/to/uthash/utarray.h', needed by `HelloTest'. Stop.
I have been struggling for some days now to build a simple 'Hello World' programme which mixes C and C++, and pulls in various external libraries using CMake. For full disclosure it should be known that I am fairly new to C, C++ and CMake hence please be nice.
I am working in OS X Yosemite 10.10.4 (my googling seems to suggest this might be part of the problem). I am working out of the CLion IDE.
Alas, here we go, here is the programme I am trying to build:
#include <iostream>
#include "Simbody.h" \\Written in C++
extern "C"
{
#include "project_in_C.h"
}
using namespace std;
using namespace SimTK;
int main(int argc, char** argv) {
cout << "Hello, World!" << endl;
return 0;
}
We have a physics library written in C++ a bespoke project written in C, for which the main header file is, lets call it; project_in_C.h.
Again; all I am trying to do is build a simple mixed C/C++ project.
Now, the above is executed using CMake, and the following CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2)
project(HelloTest)
# Simbody
find_package(Simbody REQUIRED)
include_directories(${Simbody_INCLUDE_DIR})
link_directories(${Simbody_LIB_DIR})
# Project C Headers
set(PROJC_ROOT_DIR "/Users/usr/project-c")
set(PROJC_INCLUDE_DIR ${PROJC_ROOT_DIR}/src
${PROJC_ROOT_DIR}/dir1
${PROJC_ROOT_DIR}/dir2)
include_directories(${PROJC_INCLUDE_DIR})
# Check that it has found the most important header
find_path(projFound project_in_C.h PATHS "/Users/usr/project-c/src")
if(NOT projFound)
message(FATAL_ERROR "Cannot find folder containing project_in_C.h")
endif()
# Project C Source [we want to avoid globbing]
set(PROJC_SOURCE ${PROJC_ROOT_DIR}/src/file1.c
${PROJC_ROOT_DIR}/src/file2.c
${PROJC_ROOT_DIR}/src/file3.c
${PROJC_ROOT_DIR}/src/file4.c
${PROJC_ROOT_DIR}/dir1/file5.c)
# Make library from source files
add_library(PROJC_LIBRARIES ${PROJC_SOURCE})
# Tie together
set(SOURCE_FILES main.cpp)
add_executable(HelloTest ${SOURCE_FILES})
target_link_libraries(HelloTest ${Simbody_LIBRARIES} ${PROJC_LIBRARIES})
So far so good, but here is where the truly mysterious problem arises. Upon build this is what I get in return:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/usr/Library/Caches/clion10/cmake/generated/c1d0f54d/c1d0f54d/Debug --target all -- -j 2
Scanning dependencies of target HelloTest
[ 91%] Built target PROJC_LIBRARIES
[100%] Building CXX object CMakeFiles/HelloTest.dir/main.cpp.o
Linking CXX executable HelloTest
Undefined symbols for architecture x86_64:
"_program_execution_wrapper", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [HelloTest] Error 1
make[1]: *** [CMakeFiles/HelloTest.dir/all] Error 2
make: *** [all] Error 2
But what the flippin' dynamite does that actually mean?
It is kicking up a fuss with project_in_C.h where the section the error seems to be referring to is written as so:
int program_execution_wrapper(int argc, char **argv);
int __program(int argc, char **argv);
#define main main(int argc, char **argv) { return program_execution_wrapper(argc, argv); } int __program
As ever, any help greatly appreciated.
Em...Undefined symbols is _program_execution_wrapper ... I don't see this symbol anywhere in you program. Is this maybe typo in your post? what if you change code in project_in_C.h from prog_exec_wrapper to program_execution_wrapper? And also, this function is not implemented in your code. For a start try something like this:
int program_execution_wrapper(int argc, char **argv) { printf("%s", "something"); }
Edit based on your comments:
See the following example:
We have two files (main.cpp)
#include <iostream>
int sum(int a, int b);
int main()
{
int a = sum(3, 5);
std::cout << a << std::endl;
return 0;
}
and file sum.cpp
#include <iostream>
int sum(int a, int b)
{
return a+b;
}
So I've I would like to compile this program...it would be like this
g++ main.cpp
I get the following error:
Undefined symbols for architecture x86_64:
"sum(int, int)", referenced from:
_main in main-22f955.o
ld: symbol(s) not found for architecture x86_64
But if I include sum.cpp in my main program, then program will work as charm.
I have looked all over Stack Overflow and other websites about this famous error, and all of them are very specific, and in my case I cannot find a solution. I am making an ncurses application and when i try to compile it, it causes the following error:
Undefined symbols for architecture x86_64:
"NCRS::End()", referenced from:
_main in crspro-85eaaf.o
"NCRS::Start()", referenced from:
_main in crspro-85eaaf.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I compile the code with the following line:
$ clang++ crspro.cpp -lncurses -o crspro
Here is the code:
crspro.cpp
#include "ncrs.h"
int main(int argc, char* argv[]) {
NCRS::Start();
getch();
NCRS::End();
return 0;
}
ncrs.h
#ifndef NCRS_H
#define NCRS_H
#include <ncurses.h>
#include <string>
typedef std::string string;
class NCRS {
private:
static bool __curses_on;
static bool __buffer;
static bool __echo;
static bool __keypad;
public:
static void Start(bool bbuffer=false, bool becho=false, bool bkeypad=false);
static void End();
};
#endif
ncrs.cpp
#include "ncrs.h"
static void NCRS::Start(bool bbuffer=false, bool becho=false, bool bkeypad=false) {
initscr();
if (bbuffer) raw();
if (becho) echo(); else noecho();
if (bkeypad) keypad(stdscr, TRUE); else keypad(stdscr, FALSE);
__buffer = bbuffer;
__echo = becho;
__keypad = bkeypad;
__curses_on = true;
}
static void NCRS::End() { nocbreak(); echo(); keypad(stdscr, FALSE); endwin(); }
I don't have any issues in the code itself as far as I can tell. I have tried even including ncrs.cpp (The horror!!) but I still get the same problems.
Can anyone help with this issue? I've had this problem before with other projects and I've had to abandon them because I couldn't find a solution.
Thanks to anyone who can help!
_
_
EDIT
compile with:
clang++ crspro.cpp ncrs.cpp -lncurses -o crspro
returns error:
Undefined symbols for architecture x86_64:
"NCRS::__curses_on", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
"NCRS::__echo", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
"NCRS::__buffer", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
"NCRS::__keypad", referenced from:
NCRS::Start(bool, bool, bool) in ncrs-e52041.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Your compilation isn't including anything from ncrs.cpp, which is where both NCRS::Start() and NCRS::End() are defined. You probably want
clang++ crspro.cpp ncrs.cpp -lncurses -o crspro
Or if you want to build the object files separately and then link them:
clang++ -c crspro.cpp -c
clang++ -c ncrs.cpp -c
clang++ crspro.o ncrs.o -lncurses -o crspro
Your next error about "NCRS::__curses_on" is because you're using static variables without defining them you need to add
bool NCRS::__curses_on=false;
bool NCRS::__buffer=false;
bool NCRS::__echo=false;
bool NCRS::__keypad=false;
to one of your .cpp files. (presumably ncrs.cpp is the logical place.)
It's probably worth thinking about whether they should be static (and whether the functions should be static too) - they may need to be, but static class variables are essentially global variables, which will often come back to bite you later. They make it harder to understand the flow of the code, and can make multi-threading and testing painful.
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.