Why is the library not included? - include

Visual Studio Code project
CMake connects files
As a beginner I want to integrate testlib.h
and testlib.c which I can call in the main.c
testlib.c and .h should also call boot.h - but this doesn't work!
Including my teslib.h and testlib.c in main,c works. But In testlib.h I need access e.g. to boot.h ... but I cannot include this file!
Here is the code:
#include "testlib.h"
#include <stdint.h>
#include "boot.h" // why there comes an error when I want to include this?
#include <stddef.h>
int f123(void){
int a = 2;
return a;
}
void print_uart_22(const char text[])
{
int a;
(void)a;
(void)text;
}
Why there comes the error message?
There must be something wrong with the paths...
but main.c is next to testlib.h
main.c also includes "boot.h" without error
/testlib.c:4:10: fatal error: boot.h: No such file or directory
[build] #include "boot.h"
[build] ^~~~~~~~
[build] compilation terminated.
Why?
hahaaha
main.c is located next to testlib.h and testlib.c!
main.c includes also boot.h and there is no error.

Related

Error while building a static Linux binary (with musl-libc) that includes LuaJIT

I've cloned the LuaJIT git repo and built it with:
make STATIC_CC="musl-gcc" BUILDMODE="static"
Then, I compiled a simple Lua "hello world" script into a C header file:
luajit -b test.lua test.h
test.h:
#define luaJIT_BC_test_SIZE 52
static const unsigned char luaJIT_BC_test[] = {
27,76,74,2,10,45,2,0,3,0,2,0,4,54,0,0,0,39,2,1,0,66,0,2,1,75,0,1,0,20,72,101,
108,108,111,32,102,114,111,109,32,76,117,97,33,10,112,114,105,110,116,0
};
After that, I wrote a simple C wrapper by following the official example, test.c:
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "test.h"
int main(void) {
int error;
lua_State *L = lua_open();
luaL_openlibs(L);
error = luaL_loadbuffer(L, (const char *) luaJIT_BC_test, luaJIT_BC_test_SIZE, "test") || lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1);
}
lua_close(L);
return 0;
}
But when I try to build it, it crashes with an error:
$ musl-gcc -static -ILuaJIT/src -LLuaJIT/src -o test test.c -lluajit
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/libgcc_eh.a(unwind-dw2-fde-dip.o): in function `_Unwind_Find_FDE':
(.text+0x1953): undefined reference to `_dl_find_object'
collect2: error: ld returned 1 exit status
It's related to libgcc, so I tried building everything with musl-clang, but still got the same error. Can someone explain what I'm missing here?
Figured it out - I needed to build LuaJIT with TARGET_XCFLAGS=-DLUAJIT_NO_UNWIND like so:
make STATIC_CC="musl-gcc" BUILDMODE="static" TARGET_XCFLAGS=-DLUAJIT_NO_UNWIND
I guess this just disables C++ exceptions support, but I'm not sure what the real implications are. Seems to work fine, for now.

Undefined function from static library

I am trying to build a static library using MinGW.
Everything was going fine until I tried to use the library and got an error saying that add_numbers is an undefined function.
Many other people have had this problem and sorted it out by moving their library to be linked after the source files were included, but that was how I had written my batch file anyway, so that was not of much help.
Here are my sources.
mylib.h
#ifndef MYLIB_H
#define MYLIB_H
int add_numbers(int a, int b, int c);
#endif
mylib.c
#include "mylib.h"
int add_numbers(int a, int b, int c)
{
return a+b+c;
}
I'm building my .a file with the following commands
gcc --std=c89 -c mylib.c -o mylib.o
ar rcs libmylib.a mylib.o
I've also tried with out specifying the standard.
There are no errors or warnings when running this command.
Next, my test program looks like this.
#include <stdio.h>
#include "mylib.h"
int main()
{
printf("The sum of 1, 2, and 3 is %d", add_numbers(1, 2, 3));
getchar();
return 0;
}
And lastly, we build the test with this command.
gcc mylibtest.c -L -lmylib -o test.exe
I've tried moving around those commands into many many different sequences, but always receiving the following error:
C:\Users\Aaron\AppData\Local\Temp\cc0ERpBi.o:mylibtest.c:(.text+0x26): undefined
reference to `add_numbers'
collect2.exe: error: ld returned 1 exit status
E:\my_first_static_library>
Any help would be very appreciated, I've read every tutorial I could find on the art of writing static libraries, as well as a good ten stackoverflow questions.
You are missing a dot after -L:
gcc mylibtest.c -L . -lmylib -o test.exe

libluajit-5.1.so.2:cann't open shared object file:No such file or directory

I want to test luajit's c api on my hosts,following is my code:
#include <stdio.h>
#include <luajit.h>
#include <lualib.h>
#include <lauxlib.h>
int main()
{
lua_State *L;
L=luaL_newstate();
luaL_openlibs(L);
lua_pushnumber(L,10);
lua_pushstring(L,"hello");
lua_pushboolean(L,0);
lua_close(L);
//printf("luatop:%d\n",lua_gettop(L));
return 0;
}
Then I compile it with gcc:
gcc -I /usr/local/include/luajit-2.0/ -lluajit-5.1 test_lua.c -o test_lua
But when I run it
$ ./test_lua
it prompts that,
./test_lua: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
Somebody know how to solve it?
set up LD_LIBRARY_PATH variable, make it reference to libluajit-5.1.so.2 live directory.
cmd:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

communication between a cpp file and an h file

Please consider the following
/*utils.h*/
#include <cstdio>
#include <iostream>
//#include some other files, including <string>
//ifndef ... and such macro
extern string configpath;
extern void writelog(string, string);
extern string get_fromfile(string, string);
//end the said macro
then we have
/*utils.cpp*/
//all necessary includes, including <string>
#include "utils.h" //they are in the same folder, as the following main .cpp
void writelog(string msg, string location = "lookinconfigfile")
{
if (location == "lookinconfigfile")
{
get_fromFile(configpath, "logpath");
//the function correctly returns the path to logfile, tested separatly.
}
...
}
string get_formFile(string flpt, string wht)
{...}
then in main.cpp, i include utils.h, and set the configpath to point to a file which holds a path to the logfile.
now g++ -c utils.cpp -std=c++11 produces utils.o
g++ -c main.cpp -std=c++11 produces main.o
before introducing the configpath as a global variable, by explicitly mentioning the configpath, that is, hard coding it to each occurrence, i was able to do this:
g++ main.o -o main
and that would generate main as an executable which would behave as expected.
But now i get this:
main.o: In function `writelog(std::string, std::string)':
main.cpp:(.text+0x2ce): undefined reference to `configpath
Also, if i keep the definition of get_fromFile in utils.cpp _after_ writelog, despite the prototype in utils.h, i get that get_fromFile is not defined.
where do i search looking for solutions?
edit: as user2079303 suggested, yes, it is utils.h, not utils.cpp, thank you.
edit2: as bobah mentioned, i actually have the cases correct in my code, just mistyped here. sorry.
You need to define your variable to let compiler know which object file to put it to. Mentioning it in the header file as extern string configpath; just tell compiler that somewhere there will be this variable, leave unresolved reference and let linker resolve it.
Add to your main.cpp:
string configpath;
In your final invocation to gcc, you only link main.o, but not utils.o.

CUDA and Thrust library: Trouble with using .cuh .cu and .cpp files together with -std=c++0x

I want to have a .cuh file where I can declare kernel functions and host functions as well. The implementation of these functions will be made inside the .cu file. The implementation will include the use of the Thrust library.
In the main.cpp file I would like to use the implementation that is inside the .cu file. So let's say we have something like this:
myFunctions.cuh
#include <thrust/sort.h>
#include <thrust/device_vector.h>
#include <thrust/remove.h>
#include <thrust/host_vector.h>
#include <iostream>
__host__ void show();
myFunctions.cu
#include "myFunctions.cuh"
__host__ void show(){
std::cout<<"test"<<std::endl;
}
main.cpp
#include "myFunctions.cuh"
int main(void){
show();
return 0;
}
If I compile by doing this:
nvcc myFunctions.cu main.cpp -O3
And then run the executable by typing ./a.out
The test text will be printed.
However, if I decide to include -std=c++0x by using the following command:
nvcc myFunctions.cu main.cpp -O3 --compiler-options "-std=c++0x"
I get a lot of errors, some of which are the following:
/usr/include/c++/4.6/x86_64-linux-gnu/./bits/c++config.h(159): error: identifier "nullptr" is undefined
/usr/include/c++/4.6/x86_64-linux-gnu/./bits/c++config.h(159): error: expected a ";"
/usr/include/c++/4.6/bits/exception_ptr.h(93): error: incomplete type is not allowed
/usr/include/c++/4.6/bits/exception_ptr.h(93): error: expected a ";"
/usr/include/c++/4.6/bits/exception_ptr.h(112): error: expected a ")"
/usr/include/c++/4.6/bits/exception_ptr.h(114): error: expected a ">"
/usr/include/c++/4.6/bits/exception_ptr.h(114): error: identifier "__o" is undefined
What do these errors mean and how can I avoid them?
Thank you in advance
If you look at this specific answer, you'll see the user is compiling an empty dummy app with the same switch you are using and getting some of the exact same errors. If you restrict the usage of that switch to compiling .cpp files, you'll probably have better results:
myFunctions.h:
void show();
myFunctions.cu:
#include <thrust/sort.h>
#include <thrust/device_vector.h>
#include <thrust/remove.h>
#include <thrust/host_vector.h>
#include <thrust/sequence.h>
#include <iostream>
#include "myFunctions.h"
void show(){
thrust::device_vector<int> my_ints(10);
thrust::sequence(my_ints.begin(), my_ints.end());
std::cout<<"my_ints[9] = "<< my_ints[9] << std::endl;
}
main.cpp:
#include "myFunctions.h"
int main(void){
show();
return 0;
}
build:
g++ -c -std=c++0x main.cpp
nvcc -arch=sm_20 -c myFunctions.cu
g++ -L/usr/local/cuda/lib64 -lcudart -o test main.o myFunctions.o

Resources