abhiTronix / raspberry-pi-cross-compilers

Latest GCC Cross Compiler & Native (ARM & ARM64) CI generated precompiled standalone toolchains for all Raspberry Pis. 🍇
https://sourceforge.net/projects/raspberry-pi-cross-compilers
GNU General Public License v3.0
594 stars 104 forks source link

Hello World cmake with toolchain example? #60

Closed oosavu closed 3 years ago

oosavu commented 3 years ago

Hello!!! My enviroment is:

toolchain_s.cmake:

set(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_SYSTEM_PROCESSOR armhf)

set(tools /home/oosavu/cv/cross-pi-gcc-8.3.0-0)
set(rootfs_dir /home/oosavu/cv/rootfs)

set(CMAKE_C_COMPILER ${tools}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/arm-linux-gnueabihf-g++)

SET(CMAKE_FIND_ROOT_PATH ${rootfs_dir})
SET(CMAKE_SYSROOT ${rootfs_dir})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

SET (CMAKE_C_COMPILER_WORKS 1)
SET (CMAKE_CXX_COMPILER_WORKS 1)

CMakeList.txt:

cmake_minimum_required(VERSION 3.10)
project(rpi_main)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 17)
add_executable(rpi_main main.cpp ${rpi_src})
include_directories(${rootfs_dir}/usr/include/arm-linux-gnueabihf )

main.cpp:

#include <iostream>
int main(int argc, char *argv[])
{
    std::cout << "qweqwe" << std::endl;
    return 0;
}

Error occurs on the linkage step: ld: cannot find crt1.o: No such file or directory ld: cannot find crti.o: No such file or directory ld: cannot find -lm

What i'm doing wrong?! i spent many time for migrating to this tool and it is a last obstacle. anybody please help! Do anybody show me their toolchain file?

output of make and cmake: image

abhiTronix commented 3 years ago

@oosavu Just add link_directories(${rootfs_dir}/usr/lib/arm-linux-gnueabihf) at end of your CMakeList.txt as follows:

cmake_minimum_required(VERSION 3.10)
project(rpi_main)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 17)
add_executable(rpi_main main.cpp ${rpi_src})
include_directories(${rootfs_dir}/usr/include/arm-linux-gnueabihf)
link_directories(${rootfs_dir}/usr/lib/arm-linux-gnueabihf)

and problem will be solved.

Warning ⚠️ : Also make sure you ran rsync command with sudo privileges and fixed symbolic links, as mentioned in docs at step-6 and step-7

abhiTronix commented 3 years ago

@oosavu Test solution in previous comment, and then comment your results here.

oosavu commented 3 years ago

Thanks for quick reply!

Similar bug has already present: https://github.com/abhiTronix/raspberry-pi-cross-compilers/issues/3 But unfortunately solutions from this old bug does not help for me :( (unset(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES))

So for now commenting this line in the toolchain file is only that i can do. It is not good because it brokes many benefits of cmake, like "find_library".

If you have another ideas what can i do - it will be great!

p.s.: you can merge four rsync commands into one: rsync -vR --progress -rl --delete-after --safe-links pi@192.168.0.100:/{lib,usr,opt/vc/lib} $HOME/cv/rootfs

p.s.2: it will be great to add this hello world application to you repository since it is hard to find good example for cross-compilation project!

abhiTronix commented 3 years ago

Similar bug has already present: #3 But unfortunately solutions from this old bug does not help for me :( (unset(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES))

@oosavu Apologies but I'm unable to understand if you still having bugs after SET(CMAKE_SYSROOT ${rootfs_dir}) fix or you already resolved them? If there are bugs, are they similar to #3?

So for now commenting this line in the toolchain file is only that i can do. It is not good because it brokes many benefits of cmake, like "find_library".

If you have another ideas what can i do - it will be great!

Also, by "this line" you meant "unset(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES)" ?

p.s.2: it will be great to add this hello world application to you repository since it is hard to find good example for cross-compilation project!

Sure, I'm going paste the results of hello world application in our wiki docs.

oosavu commented 3 years ago

@abhiTronix

uuups. My mistake. #3 is NOT a similar bug. Forget about it.

"this line" - is "SET(CMAKE_SYSROOT ${rootfs_dir})".

This works:

set(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_SYSTEM_PROCESSOR armhf)

set(tools /home/oosavu/cv/cross-pi-gcc-8.3.0-0)
set(rootfs_dir /home/oosavu/cv/rootfs)

set(CMAKE_C_COMPILER ${tools}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/arm-linux-gnueabihf-g++)

SET(CMAKE_FIND_ROOT_PATH ${rootfs_dir})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

SET (CMAKE_C_COMPILER_WORKS 1)
SET (CMAKE_CXX_COMPILER_WORKS 1) 

image

abhiTronix commented 3 years ago

@oosavu Nice. I'll close this once wiki docs are updated. Thank you.

fuzun commented 3 years ago

Omitting setting CMAKE_SYSROOT is not a solution. It is rather a workaround and does not work for projects where sysroot must be defined.

I think this issue is related with the toolchain. However, I don't know how it works for @abhiTronix given that I followed his guide.

abhiTronix commented 3 years ago

@fuzun Stop Hijacking other issues, and open your own or rather just quit with your bullshit. First of all, This is not the problem with Toolchains but with CMake (if you being ignorant), since CMake prefixes all paths with the CMAKE_SYSROOT value if defined, so find_library (used to find required libs for compilingmain.cpp) will be looking in the wrong places. You can easily test this by copying your entire $WORKSPACE to $CMAKE_SYSROOT/$WORKSPACE. Then, the CMake scripts worked fine, and find_library is found correctly and it compiles without any problems. Therefore as @oosavu correctly concluded, only CMAKE_FIND_ROOT_PATH is the correct way to handle the sysroot.

abhiTronix commented 3 years ago

@fuzun Bring a right attitude on the table to comment here or just begone.

abhiTronix commented 3 years ago

Closed in docs: https://github.com/abhiTronix/raspberry-pi-cross-compilers/wiki/Raspberry-Pi-GCC-Cross-Compiler-ARM-Toolchains-CMake-Usage-Guide/17f85af96eafabab415351a21d8d653bd3feca5c