library is linked but reference is undefined

when you are linking, the order of your libraries and source files makes a difference. for example for your case, g++ -I/usr/local/cuda/include -L/usr/lib/nvidia-current -lOpenCL opencl.cpp functions defined in the OpenCL library might not be loaded, since there nothing before them asking for a look-up. however if you use, g++ opencl.cpp -I/usr/local/cuda/include -L/usr/lib/nvidia-current -lOpenCL then any … Read more

Multiple definition error on variable that is declared and defined in header file and used only in its cpp file

If you declare your variable in the header file: #ifndef GLOBAL_H #define GLOBAL_H int foo = 0; #endif In every include of your header file or translation unit, a new instance of your integer is created. As you mentioned, to avoid this, you need to declare the item as “extern” in the header file and … Read more

How to solve ——-undefined reference to `__chkstk_ms’——-on mingw

Here’s an authoritative answer, from a MinGW project administrator. Your problem arises because you continue to use an obsolete, (no longer maintained; no longer supported), version of GCC. Current versions of mingwrt are compiled using GCC-4.x, (I used GCC-4.8.2 for mingwrt-3.21 and its descendants), and this introduces the dependencies on __chkstk_ms, (which is provided by … Read more

Compiling multiple C files in a program

The correct way is as follows: file1.c #include <stdio.h> #include “file2.h” int main(void){ printf(“%s:%s:%d \n”, __FILE__, __FUNCTION__, __LINE__); foo(); return 0; } file2.h void foo(void); file2.c #include <stdio.h> #include “file2.h” void foo(void) { printf(“%s:%s:%d \n”, __FILE__, __func__, __LINE__); return; } output $ $ gcc file1.c file2.c -o file -Wall $ $ ./file file1.c:main:6 file2.c:foo:6 $

How does the linker handle identical template instantiations across translation units?

C++ requires that an inline function definition be present in a translation unit that references the function. Template member functions are implicitly inline, but also by default are instantiated with external linkage. Hence the duplication of definitions that will be visible to the linker when the same template is instantiated with the same template arguments … Read more

Can I link a plain file into my executable? [duplicate]

You could do this: objcopy –input binary \ –output elf32-i386 \ –binary-architecture i386 my_file.xml myfile.o This produces an object file that you can link into your executable. This file will contain these symbols that you’ll have to declare in your C code to be able to use them 00000550 D _binary_my_file_xml_end 00000550 A _binary_my_file_xml_size 00000000 … Read more