glfw linking by cmake undefined reference error - cmake

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

Related

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.

SFML sf::RenderWindow::createWindow() crashes program

So I've been learning C++ and I've been trying to learn some SFML 2 via some videos. At first I had no real issues, I was using MS VS2012 and everything was fine. I start using MS VS2015 Community and it all starts going wrong and I've got no idea why!
Main problem:
Everything compiles, but it just crashes when I try to use sf::RenderWindow::createWindow()
Error Message:
I get the message "SFML_Project.exe is no longer working",
I go to debug it and it gives me the following message:
Unhandled exception thrown: read access violation.
this->_Ptr was 0xCCCCCCCC.
If there is a handler for this exception, the program may be safely continued.
and it does it on this function (some SFML code I know nothing about)
const facet *_Getfacet(size_t _Id) const
{ // look up a facet in locale object
const facet *_Facptr = _Id < _Ptr->_Facetcount
? _Ptr->_Facetvec[_Id] : 0; // null if id off end <- ON THIS LINE OF CODE IT BREAKS
if (_Facptr != 0 || !_Ptr->_Xparent)
return (_Facptr); // found facet or not transparent
else
{ // look in current locale
locale::_Locimp *_Ptr = _Getgloballocale();
return (_Id < _Ptr->_Facetcount
? _Ptr->_Facetvec[_Id] // get from current locale
: 0); // no entry in current locale
}
}
Line of info that was given at the Call Stack
sfml-system-d-2.dll!std::locale::_Getfacet(unsigned int _Id) Line 451 C++
My Code:
#include <iostream>
#include "SFML\Graphics.hpp"
int main()
{
sf::RenderWindow window;
window.create(sf::VideoMode(800, 800), "WindowName");
sf::Texture texture;
sf::Sprite sprite;
texture.loadFromFile("Player.png");
sprite.setTexture(texture);
sf::Event eventHandler;
while(window.isOpen())
{
while(window.pollEvent(eventHandler))
{
switch(eventHandler.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
window.clear();
window.draw(sprite);
window.display();
}
}
SFML version: Visual C++ 14 (2015) - 32-bit
Project Properties:
Debug -> C/C++ -> General -> Additional Include Directories:
$(SolutionDir)/SFML-2.3.2/include
Debug -> Linker -> General -> Additional Library Directories:
$(SolutionDir)/SFML-2.3.2/lib
Debug -> Linker -> Input -> Additional Dependencies:
sfml-main-d.lib
sfml-window-d.lib
sfml-graphics-d.lib
sfml-system-d.lib
sfml-network-d.lib
sfml-audio-d.lib
What I've tried:
I've tried turning all the dependencies from sfml-XX-d.lib to sfml-XX.lib files, which does allow me to create a window and draw shapes to that window, but then when I try to use sf::Texture::loadFromFile("filename") the console command window turns into the matrix and starts beeping.
It might be because you link the window lib before the graphics lib.
Try to move up the window lib under the graphics lib.
sfml-graphics-d.lib
sfml-window-d.lib
sfml-system-d.lib
If it's still crashing, it may because your sfml's dlls does not match the current sfml version that you have. Happened to me once, I downloaded a new version of sfml without updating the binaries.

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

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.

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.