Skip to content

Instantly share code, notes, and snippets.

@gglin001
Last active August 4, 2022 13:39
Show Gist options
  • Save gglin001/7daabeb1512ad8f4e40f293f90ff28bd to your computer and use it in GitHub Desktop.
Save gglin001/7daabeb1512ad8f4e40f293f90ff28bd to your computer and use it in GitHub Desktop.
ubuntu gcc from apt issue
cmake_minimum_required(VERSION 3.0)
project(project)
set(CMAKE_VERBOSE_MAKEFILE ON)
# a shared library
add_library(lib SHARED lib.cpp)
add_executable(main main.cpp)
# link with lib
target_link_libraries(main PRIVATE lib)
#include <iostream>
static void func() { std::cout << "\n\n\n\nfuncfucnfucn\n\n\n\n"; }
// run before main expectedly !
static int dummy = (func(), 0);
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "main" << std::endl;
return 0;
}
@gglin001
Copy link
Author

gglin001 commented Aug 4, 2022

if you compile this project using gcc/g++ from apt install gcc g++, the func() in lib.cpp will not run expectedly

> ./build/main 
main

but with manually compiled gcc or gcc from conda, yoiu will get evertything work

> ./build/main 




funcfucnfucn



main

@gglin001
Copy link
Author

gglin001 commented Aug 4, 2022

without cmake

/usr/bin/g++ lib.cpp --shared -fPIC -o liblib.so
/usr/bin/g++ main.cpp -l lib -o main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment