Error when declaring a __block variable, "Undefined symbols for architecture x86_64" - objective-c

Here's a simple program using __block variables:
typedef void (^incrementBlock)(void);
__block int incrementMe = 0;
incrementBlock add_one = ^{
incrementMe++;
};
incrementBlock add_two = ^{
incrementMe++;
incrementMe++;
};
add_one();
add_two();
printf("%d", incrementMe);
When I compile this, I get this error:
Undefined symbols for architecture x86_64: "___objc_personality_v0",
referenced from:
_main in test-b0a9a6.o
Dwarf Exception Unwind Info (__eh_frame) in test-b0a9a6.o ld: symbol(s) not found for architecture x86_64 clang: error: linker
command failed with exit code 1 (use -v to see invocation)
It definitely is a problem with declaring incrementMeas a __block int, when I comment it out it works.
I tried compiling with gcc and it also didn't work.
I got this example more or less straight from a book so it should work.
Is my declaration deprecated? Should I be declaring a mutable block variable differently?

Your code is fine, you just need to make sure to link with the Objective-C runtime library. Add -lobjc to your linker command line and you should be good.

Related

glfw linking by cmake undefined reference error

I tried to link staticly glfw lib but it's not working (idk why),so basically when i complie and run the compile throw many error mostly "undefined reference" so it's the linking error I have encounter
here's my cmake code:
cmake_minimum_required(VERSION 3.1)
project(nerd)
add_executable(${PROJECT_NAME} main.cpp)
target_link_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/../external/glfw/include)
target_link_libraries(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/../external/glfw/lib/libglfw3.a)
and here's my c++ code :
#include "../external/glfw/include/glfw3.h"
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
you can check file structre on my github : https://github.com/Solirous/glfw-help
I tried to read many document,many problem but it doesnt help
trying to link glfw:
and the error is
CMakeFiles\nerd.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x17): undefined reference to `glfwInit'
CMakeFiles\nerd.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x56): undefined reference to `glfwCreateWindow'
CMakeFiles\nerd.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x64): undefined reference to `glfwTerminate'
CMakeFiles\nerd.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x76): undefined reference to `glfwMakeContextCurrent'
CMakeFiles\nerd.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x81): undefined reference to `glfwWindowShouldClose'
CMakeFiles\nerd.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x96): undefined reference to `_imp__glClear#4'
CMakeFiles\nerd.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xa6): undefined reference to `glfwSwapBuffers'
CMakeFiles\nerd.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xab): undefined reference to `glfwPollEvents'
CMakeFiles\nerd.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xb2): undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status

io_service_open_extended link failure

I'm compiling a code using xcode. IOKit is already included in the "Link Binary with Libraries", but I still get the following errors:
Undefined symbols for architecture x86_64:
"_io_connect_method_scalarI_scalarO"
"_io_service_open_extended"
_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)
Although those two functions are defined within the program using the following code:
extern "C" kern_return_t io_service_open_extended
(
mach_port_t service,
task_t owningTask,
uint32_t connect_type,
NDR_record_t ndr,
io_buf_ptr_t properties,
mach_msg_type_number_t propertiesCnt,
kern_return_t *result,
mach_port_t *connection
);
extern "C" kern_return_t io_connect_method_scalarI_scalarO(
io_connect_t conn, uint32_t selector,
io_scalar_inband64_t scalar_input,
mach_msg_type_number_t scalar_inputCnt,
io_struct_inband_t inband_output,
mach_msg_type_number_t *inband_outputCnt
);
But those functions can be found in Apple's API reference, and can also be googled.
try this:
set the Build Active Architecture only to NO.

Building LLVM pass out of source - undefined symbols

I'm attempting to build an LLVM pass using the instructions here and link it against the copy of LLVM installed by Julia. The pass is currently being compiled successfully, but cmake fails on linking with undefined symbol errors.
[ 50%] Building CXX object VectorizePass/CMakeFiles/VectorizePass.dir/VectorizePass.cpp.o
[100%] Linking CXX shared module libVectorizePass.so
Undefined symbols for architecture x86_64:
"llvm::raw_ostream::write_escaped(llvm::StringRef, bool)", referenced from:
(anonymous namespace)::Hello::runOnFunction(llvm::Function&) in VectorizePass.cpp.o
"llvm::raw_ostream::write(char const*, unsigned long)", referenced from:
llvm::raw_ostream::operator<<(llvm::StringRef) in VectorizePass.cpp.o
There are dozens of undefined symbol errors followed by
"vtable for llvm::Pass", referenced from:
llvm::Pass::Pass(llvm::PassKind, char&) in VectorizePass.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual
member function has no definition.
VectorizePass.cpp
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace {
struct Hello : public FunctionPass {
static char ID;
Hello() : FunctionPass(ID) {}
bool runOnFunction(Function &F) override {
errs() << "Hello: ";
errs().write_escaped(F.getName()) << "\n";
return false;
}
};
}
char Hello::ID = 0;
static RegisterPass<Hello> X("Hello", "My Hello World Pass", false, false);
This is exactly as it is in the tutorial.
CMakeLists.txt (1)
add_library(VectorizePass MODULE VectorizePass.cpp)
SET(CMAKE_CXX_FLAGS "-std=c++11 -Wall -fno-rtti -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -fPIC")
CMakeLists.txt (2)
project(VectorizePass)
set(LLVM_DIR "/Users/user/julia5/deps/build/llvm-3.7.1/build_Release")
set(LLVM_TOOLS_BINARY_DIR "/Users/user/julia5/deps/build/llvm-3.7.1/build_Release/Release/bin")
include_directories(${LLVM_INCLUDE_DIRS})
include_directories("/Users/user/julia5/deps/build/llvm-3.7.1/build_Release/include")
include_directories("/Users/user/julia5/deps/srccache/llvm-3.7.1/include")
link_directories("/Users/user/julia5/deps/build/llvm-3.7.1/build_Release/Release/lib")
link_directories("/Users/user/julia5/deps/build/llvm-3.7.1/build_Release/lib")
link_directories(${LLVM_LINK_DIRS})
add_definitions(${LLVM_DEFINITIONS})
add_subdirectory(VectorizePass)
Clearly, for some reason CMake isn't finding the appropriate object files even though they are in the directories that I have in the link_directories statements. What am I missing? I'm fairly new to CMake so it may be something obvious.
I've attempted to include(AddLLVM) as suggested here but CMake reports that it cannot find AddLLVM. This Stack post also suggests using a regular Makefile but that does not work when compiling out-of-source passes as all the paths in the regular LLVM Makefile.common/Makefile.config are relative and don't work at all.
Have you read this? You need to use add_llvm_loadable_module() macro to define target for your pass.

Inline function "undefined symbols" error

I want to write an inline function, but I get an error. How can I fix it?
Error information:
Undefined symbols for architecture i386:
"_XYInRect", referenced from:
-[BeginAnimation ccTouchesEnded:withEvent:] in BeginAnimation.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Code:
CGPoint location = CGPointMake(60, 350);
if (XYInRect(location, 53, 338, 263, 369)) {
}
inline BOOL XYInRect(CGPoint location, float MixX, float MixY, float MaxX ,float MaxY){
if (location.x >MixX && location.y >MixY && location.x <MaxX && location.y <MaxY) {
return YES;
} else {
return NO;
}
}
Clang defaults to C99 and not GNU sematics, which means a raw inline is different from both static inline and extern inline.
In particular, a raw inline means that the function still has external linkage, but the inline definition does not provide the external one (you'd need extern inline for that).
What this means is that you need an additional extern definition in a different translation unit or linking will fail. However, you're probably looking for static inline.
I think XYInRect() needs to be known prior to being used. Move the function definition to some place in the file before you call it.

Why do static functions eliminate undefined symbols in Xcode?

I am attempting to use I/O kit and have linked to I/O kit properly.
When I use a function in I/O kit and don't call it within a static function, I get the following error Undefined symbols for architecture x86_64.
Here is an example to suppress the error
static void test(void)
{
if (IORegisterForSystemPower(...))
{
}
}
Here is an example that will cause the error.
void test(void)
{
if (IORegisterForSystemPower(...))
{
}
}
Any suggestions as to why this is happening?
EDIT:
Here are the exact error messages:
Undefined symbols for architecture x86_64:
"_IORegisterForSystemPower", referenced from:
_registerNotificaitonUsingIOKit in AppDelegate.o
"_IONotificationPortGetRunLoopSource", referenced from:
_registerNotificaitonUsingIOKit in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Okay I got one scenario when this can happen. If the static function is never called, you won't get that link time error.
For example, I wrote a simple c file with this function, and undef_foobar is not defined:
static int foobar (void)
{
undef_foobar ();
}
Now, if foobar() is called from my main(), I get the error:
Undefined symbols for architecture x86_64:
"_undef_foobar", referenced from:
If the function isn't called at all from within this c file, there are no linker errors.